How to get multiple parameters with the same key in a JSF managed bean

Say I have this query:

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 called twice, so I can add items to the List , but it was only called once, with the first value ( par1 ).

I also tried converting b to List<String> , but JSF is not evaluated as List .

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

+4
source share
1 answer

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

With @ManagedProperty it's pretty simple. You need #{paramValues.b} instead of #{param.b} . This is done under covers just like HttpServletRequest#getParameterValues() , which returns a String[] with all parameter values ​​for a given name.

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

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


Update : coincidentally. I met the following comment in the decode() method decode() in the UIViewParameter source (Mojarra 2.1.1, line 218 onwards), and I thought about this question again:

 // 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 is "by design" just not supported on <f:viewParam> .

+5
source

All Articles