f:viewParam with multiple values

百般思念 提交于 2019-12-30 20:02:07

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/7794149/fviewparam-with-multiple-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!