Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

f:viewParam with multiple values

Tags:

jsf

viewparams

I have a <f:viewParam> driven search screen. I am trying to implement it to take multiple values for a single <f:viewParam>.

I believe the correct URL would look something like

<url>?state=COMPLETE&state=PENDING

The XHTML section is as follows:

<f:metadata>
   <f:viewParam name="state"
      value="#{backingBean.state}"
      converter="#{stateNameConverter}" />
</f:metadata>

I have tried the following 2 method signatures on backingBean:

public void setState(State... state)

Was hopeful that JSF implementation would build an array for the values and set on the backing bean. JSF implementation failed with error stating it cannot convert enum to array of enums.

public void setState(State state)

Thought that maybe the JSF implementation would set the converted values as it found them in the URL. Only the first value was set.

The stateNameConverter converts between String and enum value.

Is it possible to have multiple values for <f:viewParam> in JSF 2?

like image 564
fransvn Avatar asked Oct 17 '11 13:10

fransvn


1 Answers

No, unfortunately, there's no such tag as <f:viewParams>. There's also a comment in Mojarra's UIViewParameter#decode() implementation (which is the code behind <f:viewParam> tag) which confirms that:

@Override
public void decode(FacesContext context) {
    if (context == null) {
        throw new NullPointerException();
    }

    // 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());

    // submitted value will stay as previous value (null on initial request) if a parameter is absent
    if (paramValue != null) {
        setSubmittedValue(paramValue);
    }

    rawValue = (String) getSubmittedValue();
    setValid(true);

}

Right now your best bet to workaround this is by gathering the values yourself from ExternalContext#getRequestParameterValuesMap() in a @PostConstruct or <f:viewAction> or maybe a @ManagedProperty("#{paramValues.state}") on a String[] property.

You could also create a custom <my:viewParams> tag which achieves that, but this isn't exactly trivial.

like image 81
BalusC Avatar answered Nov 15 '22 05:11

BalusC