f:viewParam具有多个值 [英] f:viewParam with multiple values
问题描述
我有一个<f:viewParam>
驱动的搜索屏幕.我试图将其实现为单个<f:viewParam>
采用多个值.
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
XHTML部分如下:
The XHTML section is as follows:
<f:metadata>
<f:viewParam name="state"
value="#{backingBean.state}"
converter="#{stateNameConverter}" />
</f:metadata>
我已经在backingBean上尝试了以下2种方法签名:
I have tried the following 2 method signatures on backingBean:
public void setState(State... state)
希望JSF实现将为这些值构建一个数组并在后备bean上进行设置. JSF实施失败,出现错误,指出它无法将枚举转换为枚举数组.
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)
认为JSF实现可能会在URL中找到转换后的值时对其进行设置.仅设置了第一个值.
Thought that maybe the JSF implementation would set the converted values as it found them in the URL. Only the first value was set.
stateNameConverter
在String
和enum
值之间转换.
在JSF 2中是否可以为<f:viewParam>
提供多个值?
Is it possible to have multiple values for <f:viewParam>
in JSF 2?
推荐答案
不,不幸的是,没有像<f:viewParams>
这样的标签. Mojarra的 UIViewParameter#decode()
实现(这是<f:viewParam>
标记后面的代码),它确认:
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);
}
现在最好的解决方法是,自己从@PostConstruct
或<f:viewAction>
中的ExternalContext#getRequestParameterValuesMap()
或String[]
属性中的@ManagedProperty("#{paramValues.state}")
收集值.
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.
您还可以创建一个自定义的<my:viewParams>
标记来实现此目的,但这并不简单.
You could also create a custom <my:viewParams>
tag which achieves that, but this isn't exactly trivial.
这篇关于f:viewParam具有多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!