java 我可以“传递”带有 JSF param 标签的对象吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2082958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I "pass" an object with a JSF param tag?
提问by John M Naglick
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).
我正在寻找的是有一个 f:param 标记,其中包含我自己在 value 属性中选择的对象。然后,在操作的支持 bean 方法中,我希望能够从请求中提取此对象。(对不起,如果我的术语不太好,我是 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 reallypassing 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.
现在,我可以在请求参数中传递字符串就好了。我也意识到参数在 http get 或 post 中总是会是一个字符串,所以我并没有真正传递一个 java 对象。我也意识到这样做的一种方法是传递某种类型的“id”,然后支持 bean 可以使用它来识别有问题的对象。
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?
然而,我想知道的是 JSF 是否允许我透明地执行此操作。我可以指定任何对象作为参数的值,然后在操作方法中从 RequestMap 中获取它吗?
回答by BalusC
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.
你不能用<f:param>. 它需要附加到请求的 URL 中,因此它确实需要是一个String. 换用就好了<f:setPropertyActionListener>。
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.otherBeaninside 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 injectionwith #{param.name}instead).
这样#{otherBean}就可以作为this.otherBean内部submit()方法使用。这样,您也不必惹请求参数地图(对于这情况下<f:param>,我宁愿使用了托管属性注射用#{param.name}代替)。
Alternatives are using <h:inputHidden>in combination with a Converteror using Tomahawk's <t:saveState>. Also see this blog articlefor more background info and examples.
替代方案是<h:inputHidden>与 a 结合使用Converter或使用 Tomahawk 的<t:saveState>. 另请参阅此博客文章以获取更多背景信息和示例。

