0

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>
3
  • 3
    You're not running the code you think you're running. The error is coming from a different converter which is not visible in the information provided so far. Perhaps you've created a @FacesConverter(forClass=String.class) which thus automatically runs on every single String property? Commented Apr 2, 2013 at 14:34
  • BalusC, thanks for answer. You are right. I have message in sys.out "detail=(Cannot parse this date!)" and one DateConverter from another lesson. After change in DateConverter from @FacesConverter to @FacesConverter(value = "dateConverter") all works as expect. But why this converter was applied to inputText without any converters? Commented Apr 2, 2013 at 14:48
  • Because of the forClass. Commented Apr 2, 2013 at 14:52

1 Answer 1

2

The cause of the problem is not visible in the code posted so far, but the key symptom "it fails with a message coming from a so far unidentified converter while it succeeds with an explicit converter" suggests that you've elsewhere in the same project a @FacesConverter(forClass=String.class) which would run automatically on every single String property which doesn't have another converter explicitly specified.

Sign up to request clarification or add additional context in comments.

2 Comments

I did not have any forClass converters but your suggestion has helped me
If you don't specify any explicit converter name or class like so @FacesConverter public class FooConverter {}, then it defaults to String class and thus get applied on every single String input which doesn't have an explicit converter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.