1

我正在设置一个 primefaces 选择列表,源和目标列表保持为空。

页面.xhtml

<h:form>
    <p:pickList id="picklist" value="#{bean.users}" var="user" 
        itemValue="#{user}" itemLabel="#{user.login}" converter="userConverter">  

    <f:facet name="sourceCaption">Source</f:facet>  
    <f:facet name="targetCaption">Target</f:facet>  

    <p:ajax event="transfer" listener="#{bean.onTransfer}" />    

    <p:column style="width:75%;">  
        <h:outputText value="#{user.login}" />  
    </p:column>  

    </p:pickList>

    <h:commandButton id="submit" type="submit" actionListener="#{bean.submit}" value="Submit" />
    <h:commandButton id="display" actionListener="#{bean.displayTargetList}" value="Display Target List" />
</form>

bean.java

@ManagedBean(name = "bean")
@ViewScoped
public class Bean {

    private List<User> source;
    private List<User> target;
    private DualListModel<User> users;

    public void setTarget(List<User> list) {
        this.target = list;
    }

    public void setSource(List<User> list) {
        this.source = list;
    }

    public void setUsers(DualListModel<User> list) {
        this.users = list;
    }

    public DualListModel<User> getUsers() {
        return this.users;
    }

    public List<User> getSource() {
        return this.source;
    }

    public List<User> getTarget() {
        return this.target;
    }

    @PostConstruct
    public void init() {
        this.source = myController.getUsers(); // return all my users in database
        this.target = new ArrayList<User>();
        this.users = new DualListModel<User>(this.source, this.target);
    }

    public void submit() {

    }

    public void displayTargetList() {
        System.out.println("Entering function");
        Iterator<User> iterator = this.target.iterator();
        while(iterator.hasNext()) {
            User u = iterator.next();
            System.out.println(u.getLogin());
        }
    }

}

还有转换器

用户转换器.java

@FacesConverter("userConverter")
public class UserConverter implements Converter{

    private List<User> users;

    public UserConverter(){
        this.users = MyController.getAllUsers();
    }

    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        return this.getUserById(value);
    }

    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return ((User) value).getId();
    }

    public User getUserById(String id) {
        // return the user searching from this.users
    }
}

我的代码有点复杂,但我希望错误就在这里。对我来说,它似乎像 primefaces 展示一样被实施。我可以添加转换器中的方法 getAsObject 返回一个对象(我尝试在返回值之前打印并且对象不为空)

如果你能帮忙。谢谢

4

0 回答 0