4

Let's say I have this request:

myview.xhtml?a=1&b=par1&b=par2

In myview.xhtml

<f:metadata>
  <f:viewParam name="a" value="#{myBean.a}"/>
  <f:viewParam name="b" value="#{myBean.b}"/>
</f:metadata>

In MyBean

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

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

I thought that setB(String b) would be invoked twice, so I can add the items to a List, but it was invoked just once, with the first value (par1).

I also tried to transform b into a List<String> but JSF is not evaluating as a List.

So, my question is how to inject multiple parameter values with the same key, using @ManagedProperty. (right now I am getting the paramterValues manually)

bluefoot
  • 10,220
  • 11
  • 43
  • 56

1 Answers1

5

Your question is a bit confusing. You are using both <f:viewParam> and @ManagedProperty. Usually you use the one or the other.

With @ManagedProperty this is pretty easy. You need #{paramValues.b} instead of #{param.b}. This does under the covers the same as HttpServletRequest#getParameterValues() which returns a String[] with all parameter values on the given name.

@ManagedProperty("#{paramValues.b}")
private String[] b;

With <f:viewParam> I don't see any ways. I have the impression that this is simply not supported. But I have also the impression that you don't need it at all.


Update: by coincidende I encountered the following comment in the decode() method while crawling in the source of UIViewParameter (Mojarra 2.1.1, line 218 and on) and I thought back to this question:

// QUESTION can we move forward and support an array? no different than UISelectMany; perhaps need to know
// if the value expression is single or multi-valued
// ANSWER: I'd rather not right now.
String paramValue = context.getExternalContext().getRequestParameterMap().get(getName());

So, it's "by design" simply not supported on <f:viewParam>.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • True, I may need only `@ManagedProperty` here. `"#{paramValues}` works greatly, thanks (+1). But, what should I use when I am qualifying my beans as `@Named`, since `@ManagedProperty`'s javadoc says: _If this annotation is present on a class that does not have the ManagedBean annotation, the implementation must take no action on this annotation._ ? – bluefoot Jul 05 '11 at 22:51
  • No idea. I have not looked at CDI closely. – BalusC Jul 06 '11 at 04:38
  • Well probably it will depend on the implementation. Since I'm using Spring to wire my `@Named` beans, then something like `@Inject @Named("#{param.a}")` will never work. Anyway, this ain't of the scope of this question... – bluefoot Jul 06 '11 at 13:14