I am trying to pass an object from one page to another page where each page is in a different view.
On the first page I have an input text, where myBean is a ViewScoped Bean and name is an object.
<h:inputText value="#{myBean.name}"/>
The second page contains,
<f:metadata>
<f:viewParam name="userId" value="#{myBean.name}"/>
</f:metadata>
I get the error Conversion Error setting value mypackage.myBean@257100b’ for ‘null Converter’.
Can we pass objects other than String values to view parameters?
Yes, you can. You just have to supply a converter which converts between the string representation of the object type behind
#{myBean.name}and the real object. The string representation is usually the unique technical/natural identifier of the object in question. For example, the table’s PK. It has got to be a string, simply because HTTP request parameters can be strings only. You can’t pass complex Java objects around in URLs. URLs are just strings.Here’s a kickoff example:
E.g. the following in the initial view:
(which generates an
<a href="edit.xhtml?id=123">Edit</a>)and the following in the linked view:
with this converter
(oversimplified; all null/number checks omitted, but you got the idea)
See also:
Update as per the comments, you actually want to fire a POST request. You don’t need a
<f:viewParam>at all. It’s for setting/validating/converting GET request parameters only. Attempting to serialize the whole object makes also no utter sense. Just use@ManagedProperty.E.g.
It’s only not bookmarkable anymore and not SEO-friendly (but that’s the nature of POST, you’re probably already for long aware of this). Note that the
#{personViewer}bean must by itself also be@ViewScoped(and thus not@ReqestScoped). You also need to make sure that you don’t navigate back with a redirect, but just a forward.