我无法让示例复选框选择 primefaces 工作

I can't get the example checkbox selection primefaces to work

我正在处理此示例数据table 选择“CheckBox”: https://www.primefaces.org/showcase/ui/data/datatable/selection.xhtml?jfwid=39489

我无法检测到问题的根源,显示效果很好,我什至尝试过不选择插入,效果很好; 好吧,我之前有一个比大型数据库更复杂的例子,我遇到了同样的问题。

所以我做了以下迷你 table 测试 这里是 Primefaces 代码:

 <h:form>
        <p:dataTable var="usee" value="#{user.get_all_user}" id="checkboxDT"  
                            selection="#{user.selectedUsers}" rowKey="#{user.id}"                          
                            style="margin-bottom:0" paginator="true" rows="5" selectionPageOnly="false" size="small">
            <p:column selectionMode="multiple" style="width:16px;text-align:center"/>
            <p:column headerText="id">
                <h:outputText value="#{usee.id}"></h:outputText>
            </p:column>
             <p:column headerText="nom">
                <h:outputText value="#{usee.nom}"></h:outputText>
            </p:column>
             <p:column headerText="prenom">
                <h:outputText value="#{usee.prenom}"></h:outputText>
            </p:column>
        </p:dataTable>
        <p:commandButton value="Ajouter Y" action="#{user.add_selected_users(selectedUsers)}"></p:commandButton>
        </h:form>

user.java代码:

public class user implements Serializable {

    /**
     * Creates a new instance of user
     */
    private int id;
    private String nom;
    private String prenom;
    private user selectedUser;
    private List<user> selectedUsers;

    public user getSelectedUser() {
        return selectedUser;
    }

    public void setSelectedUser(user selectedUser) {
        this.selectedUser = selectedUser;
    }

    public List<user> getSelectedUsers() {
        return selectedUsers;
    }

    public void setSelectedUsers(List<user> selectedUsers) {
        this.selectedUsers = selectedUsers;
    }

    
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }
    
    
    
    
    public user() {
    }
    public ArrayList<user> getGet_all_user() throws Exception{
        
        ArrayList<user> users = new ArrayList<user>();
         Connection connection = null;
        try{
           
            DB_connection obj_DB_connection = new DB_connection();
            connection = obj_DB_connection.get_connection();
            Statement st = connection.createStatement();
            ResultSet rs=st.executeQuery("select * from users");
            while(rs.next()){
                user obj_user = new user();
                obj_user.setId(rs.getInt("id"));
                obj_user.setNom(rs.getString("nom"));
                obj_user.setPrenom(rs.getString("prenom"));
                users.add(obj_user);                            
            }
        }catch(Exception e){
            System.out.println(e);
        }
        finally{
            if(connection!=null){
                connection.close();
            }
        }
        return users;
    
    }
    public static void add_selected_users(List<user> selectedUsers) throws Exception{
        Connection connection = null;
        try{
            DB_connection obj_DB_connection = new DB_connection();
            connection = obj_DB_connection.get_connection();
            PreparedStatement st = connection.prepareStatement("insert into users(`nom` ,`prenom` ) values(?,?)");
            for(int i=0;i<selectedUsers.size();i++){
            
            st.setString(1, selectedUsers.get(i).getNom());      
            st.setString(2, selectedUsers.get(i).getPrenom());
                 
            st.executeUpdate();
            }
        }catch(Exception e){
            System.out.println(e);
        }
        finally{
            if(connection!=null){
                connection.close();
            }
        }
    }
   
     
}

您不需要将选择传递给操作。选择绑定在您的 bean 中,因此您只需执行以下操作:

<p:commandButton value="Ajouter Y" action="#{user.addSelectedUsers}"/>
public void addSelectedUsers() {
    for (User user : selectedUsers) {
        // Do something with the selected user
    }
}

顺便说一下,您的代码不符合 类 的 Java 命名标准。 类 应以大写字母开头且名称应采用驼峰式大小写。我已经在这个答案中修复了它们。

另请参阅:

  • Java naming convention