3

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?
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Steve
  • 8,066
  • 11
  • 70
  • 112

1 Answers1

3

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

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • The bean is view scoped. Sorry, I had assumed it was implied by the fact I'm using `` which provides a way of invoking action listeners from javascript then updating part of the page. That kind of thing doesn't usually work with request scoped beans. I have started using per-page phase listeners, but it's a bit awkward at the moment. – Steve Feb 18 '12 at 03:29
  • Your best bet is then the ``. – BalusC Feb 18 '12 at 03:30
  • I started reading ViewParam vs @ManagedProperty. First of all, I did not know that `` could be used for anything other than parameters encoded into the URL. Secondly, according to that article, the view parameters are set in the Update Model Values phase, which is two phases after Apply Request Values which, I assume, is where action listeners are invoked. I'm probably way off... I'll start reading it thoroughly. Thanks. – Steve Feb 18 '12 at 03:35
  • 1
    Action listeners are invoked during Invoke Action phase :) Apply Request Values phase only sets request parameters in the view state. `` allows you to bind request parameters to the model. – BalusC Feb 18 '12 at 03:36
  • Thanks. I just added `` and it worked. I can't believe how simple it is after all that stuffing around with phase listeners and whatnot. The dual purpose of view parameters is a bit misleading. I had always though it was for GET parameters. Thanks again @BalusC. – Steve Feb 18 '12 at 03:46
  • Oh, and thank you for educating me about when action listeners are invoked. I was right about being way off. (I can always rely on being right about that!) – Steve Feb 18 '12 at 03:48
  • I just discovered something else: The view params won't be set in an AJAX postback unless their IDs are explicitly included in the list of components to execute. e.g. `` and ``. I'll add that to the long list of JSF2 gotchas. Having said that, it's understandable and entirely consistent. – Steve Feb 21 '12 at 10:54
  • what if the managed property is of complex type say `Person` and it has the properties `age` and `name` and on form post the values in the request map are `name` and `age` do i have to annotate the individual members of the `Person` bean with the `ManagedPropert` of can i just annotate the `Person` which is available in the view as `person` like `Person person` and im using `@Autowired` annotation. the person bean is inside the `PersonController` – Dakait Mar 25 '13 at 09:54