You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Jan-Kees van Andel <ja...@gmail.com> on 2010/07/19 11:32:45 UTC

JSF2 view parameters lacking?

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

Re: JSF2 view parameters lacking?

Posted by Jan-Kees van Andel <ja...@gmail.com>.
Hi,

Yeah, I know it's expected from an implementation Point of View, but from a
user's PoV I think it's very confusing and also makes viewParam less useful.
Let me explain why.

When you work according to the JSF 1.x way, you invoke the application in
the Invoke Application phase, which is not invoked when there are validation
errors. Instead, the user sees a response with an error message. This is
good.

But when you go to the webapp with a GET request, the Invoke Application
phase is not invoked, since JSF directly renders the view. So you put your
logic in a System Event listener, which is called by the preRenderComponent
or preRenderView events. This is AFAIK the recommended pattern for JSF2 GET
support. But the invocation of a System Event listener is different from an
Action Listener, which causes the 'strange' behavior, because in the end you
can't rely on anything with regards to parameters inside the listener
method. This makes it by far not as powerful as it could be because you
effectively need to write any validations or conversions yourself inside
this method.

In other words, UIInput was designed to work with POST, but UIViewParameter
extends UIInput and thus should support required, validators and converters,
but currently, using one of those concepts on a view parameter doesn't make
sense to me because it behaves strangely (again, from a user's PoV).

Or, in even different words, when you encounter a validation error in JSF
1.x, you skip the application logic, but with the idiom I sketched in the
first email, you don't skip the application logic, but directly invoke it.

BTW, I guess my workaround is also not valid, since there may already have
been some bytes written to the client. So I may not redirect to an error
page...

/JK

2010/7/19 Jakob Korherr <ja...@gmail.com>

> Hi,
>
> If you only provide param2, both params will be null in the bean, because
> param1 is required and if you don't provide it, validation will fail, and
> if
> validation fails, no value updates will occur. Thus this is the expected
> behavior.
>
> Regards,
> Jakob
>
> 2010/7/19 Jan-Kees van Andel <ja...@gmail.com>
>
> > 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
> >
>
>
>
> --
> Jakob Korherr
>
> blog: http://www.jakobk.com
> twitter: http://twitter.com/jakobkorherr
> work: http://www.irian.at
>

Re: JSF2 view parameters lacking?

Posted by Jakob Korherr <ja...@gmail.com>.
Hi,

If you only provide param2, both params will be null in the bean, because
param1 is required and if you don't provide it, validation will fail, and if
validation fails, no value updates will occur. Thus this is the expected
behavior.

Regards,
Jakob

2010/7/19 Jan-Kees van Andel <ja...@gmail.com>

> 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
>



-- 
Jakob Korherr

blog: http://www.jakobk.com
twitter: http://twitter.com/jakobkorherr
work: http://www.irian.at