Search code examples
listjsf-2convertersviewparams

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


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.


Solution

  • 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.