I try learn JSF and faced with problem.
I did use Servlet 2.5, JDK 1.6 and JSF 2.0.6 (com.sun.faces version from pom file).
I have a simple JSF page that has a <h:inputText/>
tag for interaction with user
I expect what user fill this h:inputText
then click on h:commandButton
and on server side i will get backing bean with updated value.
But in my case lifecycle of JSF breaks on process validations
, move to render
response
and show to user "Parser error!" message
I.e. for simple h:inputText
without any validator and converter i receive error message from server side about parsing of h:inputText
value.
After some time i figured out what i can create my own converter which will not modify object, just pass String
through himself.
I did add my realization of converter to <h:inputText/>
and this work.
Question:
In all examples in books and other tutorials nobody used custom converter for <h:inputText/>
if inputText
is representation of String
value of backing bean.
Why all of this tutorials and examples not working for me without custom converter? Where my mistake?
Source codes:
index.xhtml without converter, not worked for me:
<h:form id="UserForm">
<h:outputText value="Insert your first name:" />
<h:inputText id="userNameID" required="true" value="#{userBean.firstName}">
<f:validateLength minimum="5" maximum="25" />
</h:inputText>
<h:message showSummary="true" showDetail="false" for="userNameID" />
<h:commandButton id="submit" action="/view/validator/response?faces-redirect=true"
value="Submit" />
</h:form>
UserBean.java:
@ManagedBean(name = "userBean")
@SessionScoped
public class UserBean implements Serializable {
private String firstName;
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
MyConverter.java - dummy converter
@FacesConverter(value = "myConverter")
public class MyConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return value;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return value.toString();
}
}
index.xhtml with converter, worked as expected:
<h:form id="UserForm">
<h:outputText value="Insert your first name:" />
<h:inputText id="userNameID" required="true" value="#{userBean.firstName}" converter="myConverter">
<f:validateLength minimum="5" maximum="25" />
</h:inputText>
<h:message showSummary="true" showDetail="false" for="userNameID" />
<h:commandButton id="submit" action="/view/validator/response?faces-redirect=true"
value="Submit" />
</h:form>
@FacesConverter(forClass=String.class)
which thus automatically runs on every singleString
property? – BalusCsys.out
"detail=(Cannot parse this date!)" and oneDateConverter
from another lesson. After change inDateConverter
from@FacesConverter
to@FacesConverter(value = "dateConverter")
all works as expect. But why this converter was applied toinputText
without any converters? – MrusfulforClass
. – BalusC