JSF 2.0视图参数传递对象
我试图将一个对象从一个页面传递到另一个页面,其中每个页面都位于不同的视图中。
在第一页上,我有一个输入文本,其中 myBean 是一个 ViewScoped Bean,名称是一个对象。
第二页包含,
我收到错误“null Converter”的转换错误设置值 mypackage.myBean@257100b'。 我们可以传递除 String 值之外的对象来查看参数吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,你可以。您只需提供一个转换器,用于在
#{myBean.name}
后面的对象类型的字符串表示形式和真实对象之间进行转换。字符串表示通常是相关对象的唯一技术/自然标识符。比如桌子上的PK。它必须是一个字符串,因为 HTTP 请求参数只能是字符串。您无法在 URL 中传递复杂的 Java 对象。 URL 只是字符串。这是一个启动示例:
例如,初始视图中的以下内容:
(生成
Edit
)< /em>以及链接视图中的以下内容:
使用此转换器
(过于简化;省略了所有空/数字检查,但您明白了)
另请参阅:
更新 根据评论,您实际上想要触发 POST 请求。您根本不需要
。它仅用于设置/验证/转换 GET 请求参数。尝试序列化整个对象也没有任何意义。只需使用@ManagedProperty
。例如,
它只是不再可添加书签并且不利于 SEO(但这就是 POST 的本质,您可能已经意识到这一点了)。请注意,
#{personViewer}
bean 本身也必须是@ViewScoped
(因此不是@ReqestScoped
)。您还需要确保不会通过重定向向后导航,而只是向前导航。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.