John M Naglick John M Naglick - 9 months ago 47
Java Question

Can I "pass" an object with a JSF param tag?

What I'm looking for is to have an f:param tag with an Object of my own choosing in the value attribute. Then, in the backing bean method for the action, I would like to be able to pull this Object from the request. (Sorry if my terminology isnt so good, I'm new to JSF).

Now, I can pass Strings around in request parameters just fine. I also realize that a parameter is always going to be a String in the http get or post, so I'm not really passing a java object. I also realize that a way to do this would be to pass an "id" of some kind which the backing bean could then use to identify the Object in question.

What I'm wondering, however, is if JSF will allow me to do this transparently. Can I specify any object as the value of the param and then just fetch it from the RequestMap in an action method?

Answer Source

You can't do that with <f:param>. It needs to be appended to the URL of the request, so it really needs to be a String. Just use <f:setPropertyActionListener> instead.

E.g.

<h:commandLink value="Submit" action="#{bean.submit}">
    <f:setPropertyActionListener target="#{bean.otherBean}" value="#{otherBean}" />
</h:commandLink>

This way the #{otherBean} is just available as this.otherBean inside submit() method. This way you also don't need to mess with the request parameter map (for which in case of <f:param> I would rather have used managed property injection with #{param.name} instead).

Alternatives are using <h:inputHidden> in combination with a Converter or using Tomahawk's <t:saveState>. Also see this blog article for more background info and examples.