Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
965 views
in Technique[技术] by (71.8m points)

jsf 2 - How do I get selectManyCheckBox to return something other than List<String>?

This is a pattern that I would use over and over again if I get it to work. I have an enum name Log.LogKey that I want to user to pick out instances of. So the facelet has this:

             <h:form id="testForm" >

                <h:selectManyCheckbox value="#{test.selectedKeys}" >
                    <f:selectItems value="#{test.allKeys}"
                                   var="lk"
                                   itemLabel="#{lk.display}"
                                   itemValue="#{lk}" />
                </h:selectManyCheckbox>

                <h:commandButton value="Do It" action="#{test.doNothng}" />

            </h:form>

The enum has a getter called getDisplay(). The selectItems attribute calls that correctly because that's the string that gets rendered to the user. And the backing bean has this:

public class Test implements Serializable {

private List<Log.LogKey> selectedKeys = null;

public List<Log.LogKey> getAllKeys() {
    return Arrays.asList(Log.LogKey.values());
}

public List<Log.LogKey> getSelectedKeys() { return selectedKeys; }

public void setSelectedKeys(List selected) {
    System.out.println("getSelecgedKeus() got " + selected.size());
    int i = 0;
    for (Object obj : selected) {
        System.out.println(i++ + " is " + obj.getClass() + ":" + obj);
    }
}

public String doNothng() { return null; }

}

So on the form submit, the array setSelectedKeys(selected) gets called with a List of Strings, not a List of Log.LogKey. The reference to #{lk} in the selectItems tag is converting the object to a string. What would be the right way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to specify a converter. JSF EL is not aware about the generic List type because that's lost during runtime. When you do not explicitly specify a converter, JSF will not convert the submitted String values and plain fill the list with them.

In your particular case, you can make use of the JSF builtin EnumConverter, you just have to super() the enum type in the constructor:

package com.example;

import javax.faces.convert.EnumConverter;
import javax.faces.convert.FacesConverter;

@FacesConverter(value="logKeyConverter")
public class LogKeyConverter extends EnumConverter {

    public LogKeyConverter() {
        super(Log.LogKey.class);
    }

}

To use it, just declare it as follows:

<h:selectManyCheckbox value="#{test.selectedKeys}" converter="logKeyConverter">
    ...
</h:selectManyCheckbox>

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...