Is there a way to make f:viewparam handle lists of values?

六月ゝ 毕业季﹏ 提交于 2019-12-10 17:10:44

问题


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" />

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" />

Thanks!

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.


回答1:


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    }

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

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

or perhaps manually gather it from ExternalContext inside @PostConstruct.

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



来源:https://stackoverflow.com/questions/17275130/is-there-a-way-to-make-fviewparam-handle-lists-of-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!