Passing parameters to links in Facelets

I’m a JSF/Facelets newbie – so far there’s been a lot of cursing, but also some moments when I had the impression of “something done right”.

In JSF, command links are used to wire buttons to actions in backing beans. Unfortunately, you cannot pass parameters directly to the action method – but you can set parameters in your backing bean before calling the action. To set a parameter, add a f:param to the command link body – e.g.

<f:param name="myParameter" value="#{myValue}"/>

In the backing bean, the passed value can be retrieved from the faces context (similar to HttpServletRequest#getParameter for servlets). It works, but it’s inconvenient and errorprone, and requires conversion for all but string parameters.

Fortunately, there are alternatives. There’s the non-standard Tomahawk updateActionListener that directly works on bean properties. For example, you could just write

<t:updateActionListener 
   property="#{myBean.myProperty}" value="#{someValue}"/>

and forget about it. Unfortunately, Facelets doesn’t include the updateActionListener out of the box. But in JSF 1.2 there’s something even better – the setPropertyActionLister. It works exactly like the Tomahawk listener, but it’s standard and hey – it’s already been backported to Facelets 1.1:

<f:setPropertyActionListener 
   target="#{myBean.myProperty}" value="#{someValue}"/>

So far it’s worked great and it is definitely better than manually setting parameters via f:param.

Advertisements

14 Comments »

  1. Thanks for the advice on the f:setPropertyActionListener. I had a terrible time getting it to work as advertised, but a redownload of facelets fixed my troubles.

    Thanks again.

  2. Lisa said

    There are some text being fillet out when I submit comment. I have to do it again. Sorry.

    I use following codes in my jsp file:

    and in my back bean class addlayer method, I have following code to retrieved ID, nut I get null value from id.

    FacesContext fc = FacesContext.getCurrentInstance();
    String id = (String) fc.getExternalContext().getRequestParameterMap().get(”ID”);

    I don’t know why it doesn’t work. Would you pass me the code in bean class to retrieve parameter ID. Thanks

  3. @Lisa
    You’re probably not using the actual input name generated by JSF. E.g., if you have an input field with id “ID” embedded in your form “myForm”, then JSF generates an input element with name “myForm.ID”. You might also want to check the parameter map contents in a debugger.

  4. Lisa said

    Thanks for the reply.

    I used
    FacesContext fc = FacesContext.getCurrentInstance();
    String id = (String) fc.getExternalContext().getRequestParameterMap().get(”myForm:ID”);

    or
    FacesContext fc = FacesContext.getCurrentInstance();
    String id = (String) fc.getExternalContext().getRequestParameterMap().get(”myForm.ID”);

    Still, I get null value. I also test to put parameter outside of the form, and it did not work.

    I also used Iterator to walk through the parameter names and did not get my parameter from the iterator.

    Iterator i = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterNames();

    while( ( i.hasNext()))
    {
    String name = (String)i.next();

    }

    Please help.

    Lisa

  5. I think your java code is correct. Where did you place the f:param tag? For a working example, see e.g. this thread.

  6. Craig said

    Hi am i write in thinking this only works for request scoped beans?

  7. I usually use request scoped beans, but I don’t see a reason why it shouldn’t work with session or application-scoped beans.

  8. Idetrorce said

    very interesting, but I don’t agree with you
    Idetrorce

  9. Hassan said

    Good, thanks for this feature.

  10. Sudha said

    Hi,

    How do I set some values in the parameterMap
    I want to do something like
    FacesContext.getCurrentInstance().getExternalContext().
    .getRequestParameterMap().put(key,name);
    Because on some condition I want to reset the key value and call my backing bean. But this method gave me java.lang.UnsupportedOperationException.
    Is there a way to do this

    Thanks.

  11. To put a value in the request parameter map, use h:commandLink and a nested f:param instance (as described in the first part of this post).

  12. Datatable resultList said

    hi
    i new in facelt and jsf

    How to pass the Datatable resultList into backingbean

    Anybody help me
    Thanks
    palani

  13. Nigussie said

    O’h God.. You same my life… It works fine and as I want..
    Thank you very much

  14. silver_ring said

    thxxxxxxxxxxxxxxxxxxxx alot my friend 😉

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: