myfaces-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jan-Kees van Andel <jankeesvanan...@gmail.com>
Subject JSF2 view parameters lacking?
Date Mon, 19 Jul 2010 09:32:45 GMT
Hi,

On my project, we had a strange issue, caused by the way view parameters are
handled.

We had a page like this: (simplified)
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">
<f:metadata>
    <f:viewParam name="param1" value="#{testViewParamsBean.param1}"
required="true"/>
<!--        <f:convertDateTime pattern="dd-MM-yyyy"/>-->
<!--    </f:viewParam>-->
    <f:viewParam name="param2" value="#{testViewParamsBean.param2}"/>
    <f:event type="preRenderView" listener="#{testViewParamsBean.init}"/>
</f:metadata>
<h:body>
    <h:outputText value="#{testViewParamsBean.param1}"/><br/>
    <h:outputText value="#{testViewParamsBean.param2}"/><br/>
</h:body>
</html>

And a bean like this:
@ManagedBean @RequestScoped
public class TestViewParamsBean {
    private String param1, param2;

    public void init() {
        System.out.println("param1 = " + param1);
        System.out.println("param2 = " + param2);
    }
    // Getters and setters...
}

Note the required="true" attribute on the first viewParam tag in the page.
This caused the issue.

When I invoke the page with only param2 in the URL (like:
/viewParams.xhtml?param2=hello ), the init() method is invoked and the
param1 and param2 properties are both null!
When I invoke the page with both parameters present (like:
/viewParams.xhtml?param2=hello&param1=test ), both properties in the bean
are populated.
When I remove the required="true" attribute from the tag, everything works
as expected

I think this is strange behavior to say the least, but it also makes this
mechanism less useful for handling GET requests, because you don't get the
benefits of the UIViewParam, like required/validators/converters.
For example, when I add a converter to param1 (by uncommenting the tags in
the example above), I get the same behavior.

My current workaround is a state-check in the init() method, where I check
the maxSeverity of the FacesContext and redirect to an error page, but I
think this is the responsibility of the JSF lifecycle.
Maybe I'm doing something wrong, but this is the recommended behavior,
according to several JSF blogs, like:
http://andyschwartz.wordpress.com/2009/07/31/whats-new-in-jsf-2/#get-prerenderview-event

What do you guys think? Is it me or is it JSF2?

BTW, both MyFaces and Mojarra behave in this way.

Regards,
Jan-Kees

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message