How to set or inject request parameters in a managed bean?

六月ゝ 毕业季﹏ 提交于 2020-01-23 02:43:05

问题


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.

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.

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

So my question is:

  1. Is there an event type that I can handle to process the request parameters before any action listeners are invoked?
  2. Better yet, is there a way to automatically have the request parameters injected into bean properties?

回答1:


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;

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;

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}").



来源:https://stackoverflow.com/questions/9337433/how-to-set-or-inject-request-parameters-in-a-managed-bean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!