有没有一种方法可以使f:viewparam处理值列表?

编程入门 行业动态 更新时间:2024-10-28 13:27:50
本文介绍了有没有一种方法可以使f:viewparam处理值列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我已经在自定义类Car和String之间建立了一个转换器.

Let's say I already have a converter between a custom class Car and String.

有没有办法优雅地做到这一点

Is there a way to elegantly make this

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

当mybean.cars是Set<Car>或List<Car>而不是单个Car时,

可以工作,而不必在汽车列表和字符串列表之间编写自定义转换器?换句话说,JSF是否有一些功能使我不必解析"[Audi,BMW,VW]"到["Audi","BMW","VW"]然后将它们一个接一个地转换(反之亦然)?

work when mybean.cars is a Set<Car> or List<Car> instead of just a single Car, without having to write a custom converter between lists of cars and lists of strings? In other words, is there some feature of JSF that gets me out of having to parse "[Audi,BMW,VW]" to ["Audi","BMW","VW"] and then convert those one by one (and vice versa)?

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

I'd love it if there was something like

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

谢谢!

PS:我看过这篇关于转换器和selectManyMenu的帖子,在另一种情况下,实际上使用它,一切正常-selectManyMenu可以一一转换.但是现在我需要用viewParam来将列表作为URL传递.

PS: I've seen this post about converters and selectManyMenu, and in another context where I in fact use that, all works fine - selectManyMenu handles converting one by one. But right now I need viewParam to pass my list as an URL.

推荐答案

否,<f:viewParam> 不支持,也不处理具有多个值的单个参数.如果Mojarra甚至在其decode()方法:

No, the <f:viewParam> does not support nor handle a single parameter with multiple values. This is in case of Mojarra even confirmed in a comment hidden in 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中!)

Your best bet on single parameter with multiple values is to use @ManagedProperty (in a request scoped bean!)

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

或手动从@PostConstruct内部的ExternalContext收集它.

or perhaps manually gather it from ExternalContext inside @PostConstruct.

您可能要发布新的<f:viewParams>标签的功能/增强请求.

You may want to post a feature/enhancement request for a new <f:viewParams> tag.

更多推荐

有没有一种方法可以使f:viewparam处理值列表?

本文发布于:2023-11-14 13:04:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587283.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   列表   viewparam

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!