简体   繁体   English

我可以用JSF param标签“传递”一个对象吗?

[英]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. 我正在寻找的是在value属性中有一个f:param标签,其中包含我自己选择的Object。 Then, in the backing bean method for the action, I would like to be able to pull this Object from the request. 然后,在操作的支持bean方法中,我希望能够从请求中提取此Object。 (Sorry if my terminology isnt so good, I'm new to JSF). (对不起,如果我的术语不太好,我是JSF的新手)。

Now, I can pass Strings around in request parameters just fine. 现在,我可以在请求参数中传递Strings就好了。 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. 我也意识到参数总是在http get或post中成为一个String,所以我并没有真正传递一个java对象。 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. 我也意识到这样做的一种方法是传递某种“id”,然后支持bean可以使用它来识别有问题的对象。

What I'm wondering, however, is if JSF will allow me to do this transparently. 然而,我想知道的是,JSF是否允许我透明地执行此操作。 Can I specify any object as the value of the param and then just fetch it from the RequestMap in an action method? 我可以指定任何对象作为参数的值,然后在动作方法中从RequestMap中获取它吗?

You can't do that with <f:param> . 你不能用<f:param>做到这一点。 It needs to be appended to the URL of the request, so it really needs to be a String . 它需要附加到请求的URL,因此它确实需要是一个String Just use <f:setPropertyActionListener> instead. 只需使用<f:setPropertyActionListener>

Eg 例如

<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. 这样#{otherBean}就可以作为this.otherBeansubmit()方法中使用。 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). 这样你也不需要弄乱请求参数映射(对于<f:param>我宁愿使用#{param.name}来使用托管属性注入 )。

Alternatives are using <h:inputHidden> in combination with a Converter or using Tomahawk's <t:saveState> . 替代方案是将<h:inputHidden>Converter结合使用或使用Tomahawk的<t:saveState> Also see this blog article for more background info and examples. 另请参阅此博客文章以获取更多背景信息和示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM