Question

JSF 2.

A simple form with a single input that is validated. Using o:form in an attempt to fix the problem.

<f:view>
    <f:metadata>
        <f:viewParam name="info2" value="#{myClass.info2}" />
        <f:viewParam name="info1" value="#{myClass.info1}" />
        <f:event listener="#{myClass.init(}" type="preRenderView" />
    </f:metadata>
    <o:form includeViewParams="true">
        <h:outputText value="Enter some info for #{myClass.info1}" />
        <h:inputText id="input" value="#{myClass.info3}"
        validator="myValidator" />
        <br />
        <h:commandButton action="#{myClass.action()}" value="Enter some info" />
        <br />
        <h:message for="input" />
        <br />
        <h:outputText value="#{myClass.info2}" />
    </o:form>
</f:view>

Called with this URL

http://localhost:8080/test/test.xhtml?info1=hello&info2=there

When I put a value in the input field and the validation fails, the page comes back with the same URL, but with both myClass.info1 and myClass.info2 set to null. It's great that the URL is preserved, but it doesn't do much good if those parameters arent' being set in the bean.

Why is that and how do I fix it?

Was it helpful?

Solution

Unfortunately, this is expected behavior. If validations phase fails, the update model values phase won't be executed. This not only applies to the input fields of the form, but also to the view parameters involved in the same request! Apparently, your bean is request scoped and then the view parameters set during the initial request will indeed be lost.

Your best bet is making the managed bean a @ViewScoped one instead and using <o:viewParam> instead to set the view parameter during the initial GET request only. You may only need to alter the init() method to skip the job when Faces#isPostback() returns true.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top