f:selectOneMenu参数 [英] f:selectOneMenu with parameter

查看:92
本文介绍了f:selectOneMenu参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组(用户)组和一个表,其中显示了这些组(和用户).每个组表头都包含一个f:selectOneMenu,它可以设置一个组范围的值.对于本示例,将其设为int值(0、5、10、15、20)的列表.在事物的简短版本下面

I have a set of groups (of users) and a table where I display these groups (and users). Each groups table header includes a f:selectOneMenu which allows to set a group-wide value. For the sake of this example, let it be a list of int values (0, 5, 10, 15, 20). Below the short version of things

Pojos:

public class User {
    private int    id;
    private String name;
    // .. getter, setter, equals, hashCode
}  

public class Group {
    private int    id;
    private String name;
    // .. getter, setter, equals, hashCode
}

Bean:

@ManagedBean(name = "oneSelectController")
@ViewScoped
public class OneSelectController implements Serializable {

    private List<User>                users;
    private List<Group>               groups;
    private List<Integer>             values;
    private Map<Group, List<User>>    usersToGroup;
    private Map<Group, List<Integer>> valuesToGroup;

    public OneSelectController() {

        log.info("New " + this.getClass().getSimpleName() + " ...");

        users = new ArrayList<>(4);
        users.add(new User(1, "User 1"));
        users.add(new User(2, "User 2"));
        users.add(new User(3, "User 3"));
        users.add(new User(4, "User 4"));

        groups = new ArrayList<>(5);
        groups.add(new Group(1, "Group 1"));
        groups.add(new Group(2, "Group 2"));
        groups.add(new Group(3, "Group 3"));
        groups.add(new Group(4, "Group 4"));
        groups.add(new Group(5, "Group 5"));

        values = Arrays.asList(0, 5, 10, 15, 20);

        usersToGroup = new HashMap<>();
        valuesToGroup = new HashMap<>();

        for (Group g : groups) {
            usersToGroup.put(g, users);
            valuesToGroup.put(g, values);
        }
    }

    public List<Group> getGroups() {
        return groups;
    }

    public List<User> getUsers() {
        return users;
    }

    public Integer getValueByGroup(Group group) {
        // return value for given group
    }

    public List<Integer> getValuesByGroup(Group group) {
        // return list of possible values
    }
}

XHTML:

<p:dataTable id="userTable"
             var="user"
             value="#{oneSelectController.users}">
    <p:column>
        <f:facet name="header">
            <div>User</div>
        </f:facet>
        <div>
            <h:outputText value="#{user.name}"/>
        </div>
    </p:column>
    <p:columns id="groups" value="#{oneSelectController.groups}" var="group">
        <f:facet name="header">
            <div>#{group.name}</div>
            <div>
                <h:selectOneMenu value="#{oneSelectController.getValueByGroup(group)}">
                    <f:selectItems value="#{oneSelectController.getValuesByGroup(group)}" />
                </h:selectOneMenu>
            </div>
        </f:facet>
    </p:columns>
</p:dataTable>

如何为h:selectOneMenu设置有效的二传手?无论我做什么,我最终都会遇到例外.设置器(必须以某种方式)必须能够获取所选的值,但还必须包含有关针对哪个组进行选择的信息.

How do I set up a working setter for the h:selectOneMenu? Whatever I do, I end up with exceptions. The setter (somehow) must be capable to take the value selected, but also contain the information for which group the selection was made.

推荐答案

getValueByGroup视为吸气剂,因为您将其与参数一起使用.我认为这里最好的方法是使用Map,为Group类正确实现hashCode()equals()方法:

getValueByGroup is not considered a getter by JSF, since you're using it with a parameter. I think your best here is to use a Map, properly implementing the hashCode() and equals() methods for Group class:

在您的bean中:

private Map<Group, Integer> selectedValueByGroup = new HashMap<>();
//Getter

在视图中:

<h:selectOneMenu value="#{oneSelectController.selectedValueByGroup[group]}">
    <f:selectItems value="#{oneSelectController.getValuesByGroup(group)}" />
</h:selectOneMenu>

此外,与具体问题无关,正如@Kukeltje指出的那样,您也可以使用Map结构来管理按组显示的值:

Also, unrelated to the concrete question and as @Kukeltje points out, you could use a Map structure to manage the values to show by group as well:

在您的bean中:

private Map<Group, List<Integer>> valuesByGroup = new HashMap<>();
//Getter

在视图中:

<h:selectOneMenu value="#{oneSelectController.selectedValueByGroup[group]}">
    <f:selectItems value="#{oneSelectController.valuesByGroup[group]}" />
</h:selectOneMenu>

如果订购顺序对您也很重要,则可以考虑使用LinkedHashMap而不是HashMap.

You might consider using a LinkedHashMap instead of a HashMap if the ordering is important to you too.

另请参见:

  • How to set a Map value in h:inputText
  • https://www.geeksforgeeks.org/internal-working-of-hashmap-java/

这篇关于f:selectOneMenu参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆