使用单选按钮和commandButton编辑行

时间:2014-12-16 19:47:52

标签: jsf jsf-2 primefaces

我可以使用单选按钮选择单行,然后编辑该行上的一些数据然后使用 commandButton提交我在该行编辑的内容。我正在尝试编辑用户名单元格作为测试。 enter image description here

这是我的代码:

Xadmin.xhtml

<h:form id="form" enctype="multipart/form-data">
    <p:growl id="msgs" showDetail="true" />

    <p:dataTable id="DT" value="#{Jadmin.messages}"
                 var="o"
                 selection="#{Jadmin.selectedUser}"
                 rowKey="#{o.id}"
                 style="margin-bottom:20px">

        <f:facet name="header">
            Users List
        </f:facet>

        <p:column selectionMode="single" />

        <p:column>
            <f:facet name="header">
                <h:outputText value="id" />
            </f:facet>
            <h:outputText value="#{o.id}" />
        </p:column>

        <p:column>

            <f:facet name="header">
                <h:outputText value="username" />
            </f:facet>
            <p:inputText value="#{o.username}"  />
        </p:column>


        <f:facet name="footer">
            <h:commandButton  value="Update" action="#{Jadmin.update}" />

            <p:commandButton value="Delete"
                             action="#{Jadmin.delete}"
                             ajax="false"
                             update=":form:msgs"/>
        </f:facet>
    </p:dataTable>

JadminBeans.java

@ManagedBean(name = "Jadmin")
@SessionScoped
public class JadminBeans implements Serializable {

    private static final long serialVersionUID = 1L;
    private JadminController selectedUser;

    List<JadminController> userslist = new ArrayList<JadminController>();

    public List<JadminController> getMessages() {
        System.out.println("List<JadminController> getMessages()");
        userslist = JadminDAO.getAllUsers();
        return userslist;
    }

    public void delete() {
        //System.out.println(usr);
        //System.out.println(itemList.remove(item)+"!!");
        System.out.println("JadminBeans >> delete() ---------- id= ");
        JadminDAO.deleteUser(selectedUser);

    }

    public JadminController getSelectedUser() {
        return selectedUser;
    }

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

    public void update() {
        //o=(JadminBeans) objct;
        JadminDAO.updateUser(selectedUser);
    }
}

JadminDAO.java

public static void deleteUser(JadminController user) {
    try {
        PreparedStatement preparedStatement = connection.prepareStatement("delete from users where id=?");
        // Parameters start with 1
        preparedStatement.setLong(1, user.getId());
        preparedStatement.executeUpdate();
        System.out.println("JadminDAO >> deleteUser ----------");

    } catch (SQLException e) {
        System.out.println("JadminDAO >> deleteUser----------- SQLException :(");
        e.printStackTrace();
    }
}

public static void updateUser(JadminController user) {
    try {
        PreparedStatement preparedStatement = connection.prepareStatement("update users username=?, password=?, permission=? where username=?");
        // Parameters start with 1
        //System.out.println(new java.sql.Date(user.getRegisteredon().getTime()));
        preparedStatement.setString(1, user.getUsername());
        preparedStatement.setString(2, user.getPassword());
        preparedStatement.setString(3, user.getPermission());
        //preparedStatement.setDate(3, new java.sql.Date(user.getRegisteredon().getTime()));
        preparedStatement.setString(4, user.getUsername());

        preparedStatement.executeUpdate();

    } catch (SQLException e) {
        e.printStackTrace();
    }
}

JadminController.java

public class JadminController implements Serializable {

    private static final long serialVersionUID = 1L;
    private String username, password, permission;
    private long id;

    // Getters and setters.
}

1 个答案:

答案 0 :(得分:0)

如果您使用Editable Datatable,则不需要任何命令按钮来提交您已编辑过的内容。 如同一链接所示,数据表中的内部通常使用selectOneMenu而不是radioButton来进行选择。

要在数据表中使用输入元素,请不要忘记放置<f:facet name="output"></f:facet><f:facet name="input"></f:facet>

我希望它有所帮助。