如何在托管bean中设置或注入请求参数? [英] How to set or inject request parameters in a managed bean?

查看:136
本文介绍了如何在托管bean中设置或注入请求参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用多个PrimeFaces < p:remoteCommand /> 来调用页面上的各种动作侦听器。在javascript调用中,我正在传递参数。这些参数到达请求参数映射。

I'm using a number of PrimeFaces <p:remoteCommand/>s to call various action listeners on a page. In the javascript calls, I'm passing parameters. These parameters arrive in the request parameter map.

现在,我可以在动作侦听器本身中从地图中提取参数。然而,我想要的是动作听众不必这样做。相反,他们应该检查bean中的适当值是否为空并且相应地采取行动。

Now, I can extract the parameters from the map in the action listeners themselves. What I would like, however, is for the action listeners not to have to do that. Rather, they should just check that the appropriate value in the bean is not null and act accordingly.

我想要将其集中在一个事件中,或者更好的是,将请求参数值以某种方式自动注入bean。

I want to either centralize this in a single event or, better yet, have the request parameter values automatically injected into the bean somehow.

所以我的问题是:


  1. 在调用任何动作侦听器之前,是否有可以处理的事件类型来处理请求参数?

  2. 更好的是,有没有办法自动注入请求参数进入bean属性?


推荐答案

如果托管bean是请求范围的,那么你可以使用< a href =http://docs.oracle.com/javaee/6/api/javax/faces/bean/ManagedProperty.html\"rel =nofollow noreferrer> @ManagedProperty 为此。请求参数映射已在EL上下文中由#{param} 提供。

If the managed bean is request scoped, then you can use @ManagedProperty for this. The request parameter map is already in EL context available by #{param}.

@ManagedProperty("#{param.foo}")
private String foo;

如果托管bean的范围更广,则不能使用 @ManagedProperty 为此。但是,如果您正在使用CDI或可以使用它,那么您可以 homegrow 注释为了这。

If the managed bean is in a broader scope, then you can't use @ManagedProperty for this. However, if you're using CDI or can use it, then you can homegrow an annotation for this.

@Inject @HttpParam
private String foo;

更广泛范围内的JSF托管bean的替代方案是 < f:viewParam> 标签。我只能从经验中判断出它是否可以与< p:remoteCommand> 结合使用,但理论上它应该可以正常工作。另请参见 ViewParam vs @ManagedProperty(value ="#{param.id}")

An alternative for JSF managed beans in a broader scope is the <f:viewParam> tag. I can only not tell from experience if that would work in combination with <p:remoteCommand>, but in theory it ought to just work as good. See also ViewParam vs @ManagedProperty(value = "#{param.id}").

这篇关于如何在托管bean中设置或注入请求参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆