问题
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:
- Is there an event type that I can handle to process the request parameters before any action listeners are invoked?
- 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