4

假设我已经在自定义类CarString.

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

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

何时工作mybean.cars是 a Set<Car>orList<Car>而不是一个 single Car,而无需在汽车列表和字符串列表之间编写自定义转换器?换句话说,JSF 是否有一些特性让我不必解析"[Audi,BMW,VW]"然后["Audi","BMW","VW"]一一转换(反之亦然)?

如果有类似的东西我会喜欢的

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

谢谢!

PS:我看过这篇关于转换器和 selectManyMenu 的帖子,在我实际上使用它的另一个上下文中,一切正常 - selectManyMenu 处理一个一个的转换。但现在我需要 viewParam 将我的列表作为 URL 传递。

4

1 回答 1

4

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

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>标签的功能/增强请求。

于 2013-06-24T13:34:50.550 回答