有没有办法让f:viewparam处理值列表?

时间:2013-06-24 12:09:03

标签: list jsf-2 converter viewparams

假设我在自定义课程CarString之间已有转换器。

有没有办法优雅地制作这个

<f:viewParam name="mycars" value="#{mybean.cars}" converter="carConverter" />

mybean.carsSet<Car>List<Car>而不只是一个Car时工作,而无需在汽车列表和字符串列表之间编写自定义转换器?换句话说,JSF的某些功能是否让我不得不将"[Audi,BMW,VW]"解析为["Audi","BMW","VW"]然后逐个转换(反之亦然)?

如果有类似

的话,我会喜欢它
<f:viewParam name="mycars" value="#{mybean.cars}" converter="carConverter" list="true" />

谢谢!

PS:我见过this post about converters and selectManyMenu,在我实际上使用它的另一个上下文中,一切正常 - selectManyMenu逐个处理转换。但是现在我需要viewParam将我的列表作为URL传递。

1 个答案:

答案 0 :(得分:4)

不,<f:viewParam> 支持也不会处理具有多个值的单个参数。这是Mojarra甚至在隐藏在its decode() method中的评论中确认的:

213    public void decode(FacesContext context) {
214        if (context == null) {
215            throw new NullPointerException();
216        }
217
218        // QUESTION can we move forward and support an array? no different than UISelectMany; perhaps need to know
219        // if the value expression is single or multi-valued
220        // ANSWER: I'd rather not right now.
221        String paramValue = context.getExternalContext().getRequestParameterMap().get(getName());
222
223        // submitted value will stay as previous value (null on initial request) if a parameter is absent
224        if (paramValue != null) {
225            setSubmittedValue(paramValue);
226        }
227
228        rawValue = (String) getSubmittedValue();
229        setValid(true);
230
231    }

对具有多个值的单个参数最好的选择是使用@ManagedProperty(在请求范围的bean中!)

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

或者可以从ExternalContext内的@PostConstruct手动收集。

您可能希望发布新<f:viewParams>代码的功能/增强请求。

相关问题