问题描述
假设我已经在自定义类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处理值列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!