h:commandButton 在 PrimeFaces 中不起作用 p:dataTable

发布于 2024-12-01 07:20:35 字数 4329 浏览 0 评论 0原文

我有一个 PrimeFaces 数据表。对于此表中的每一行,我希望允许用户更新/删除行条目(用户)。

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.prime.com.tr/ui">
<h:head>
    <link type="text/css" rel="stylesheet" href="themes/bluesky/skin.css" />
</h:head>
<h:body>
    <center>
        <h:form>
            <p:panel id="viewUsersPanel" header="View Users">
                <p:dataTable var="user" value="#{uController.users}"
                    emptyMessage="No Users Found.">
                    <p:column style="text-align: center;">
                        <f:facet name="header">
                            <h:outputText value="Name" />
                        </f:facet>
                        <h:outputText value="#{user.name}" />
                    </p:column>
                    <p:column style="text-align: center;">
                        <f:facet name="header">
                            <h:outputText value="Postal Address" />
                        </f:facet>
                        <h:outputText value="#{user.address}" />
                    </p:column>
                    <p:column style="text-align: center;">
                        <f:facet name="header">
                            <h:outputText value="Phone Number" />
                        </f:facet>
                        <h:outputText value="#{user.phone}" />
                    </p:column>
                    <p:column style="text-align: center;">
                        <f:facet name="header">
                            <h:outputText value="Email Address" />
                        </f:facet>
                        <h:outputText value="#{user.email}" />
                    </p:column>
                    <p:column style="text-align: center;">
                        <f:facet name="header">
                            <h:outputText value="DOB" />
                        </f:facet>
                        <h:outputText value="#{user.dob}">
                            <f:convertDateTime pattern="dd-MMM-yyyy" />
                        </h:outputText>
                    </p:column>
                    <p:column style="text-align: center;">
                        <f:facet name="header">
                            <h:outputText value="No. Memberships" />
                        </f:facet>
                        <h:outputText value="#{user.numberOfMemberships}" />
                    </p:column>
                    <p:column style="text-align: center;">
                        <f:facet name="header">
                            <h:outputText value="Actions" />
                        </f:facet>
                        <h:commandButton value="Update" action="#{uController.update}" />
                        <h:commandButton value="Delete" action="#{uController.delete}" />
                    </p:column>
                </p:dataTable>
                <h:panelGrid columns="2" cellpadding="2" id="footerPanelGrid"
                    border="0">
                    <h:commandButton action="#{uController.home}" value="Home Page" />
                </h:panelGrid>
            </p:panel>
        </h:form>
    </center>
</h:body>
</html>

然而,这两个按钮都不起作用。相反,它们似乎只是刷新视图页面。我已在调试中运行该应用程序,但更新或删除方法均未命中。

我怀疑这可能是由于在 ap:dataTable 中使用 h:commandButton 造成的。但是,我也尝试过 p:commandButton 但无济于事。

作为参考,以下是 UserController 类的片段:

@ManagedBean(name="uController")
public class UserController extends AbstractController {

    private Collection<User> users;

            ...

    public String update() {

        System.out.println("Ready for update");

        return "update-user";
    }

    public String delete() {

        System.out.println("Ready for delete");

        return "delete-user";
    }
            ...
}

I have a PrimeFaces datatable. For each row in this table, I want to allow the user to update/delete the row entry (a user).

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.prime.com.tr/ui">
<h:head>
    <link type="text/css" rel="stylesheet" href="themes/bluesky/skin.css" />
</h:head>
<h:body>
    <center>
        <h:form>
            <p:panel id="viewUsersPanel" header="View Users">
                <p:dataTable var="user" value="#{uController.users}"
                    emptyMessage="No Users Found.">
                    <p:column style="text-align: center;">
                        <f:facet name="header">
                            <h:outputText value="Name" />
                        </f:facet>
                        <h:outputText value="#{user.name}" />
                    </p:column>
                    <p:column style="text-align: center;">
                        <f:facet name="header">
                            <h:outputText value="Postal Address" />
                        </f:facet>
                        <h:outputText value="#{user.address}" />
                    </p:column>
                    <p:column style="text-align: center;">
                        <f:facet name="header">
                            <h:outputText value="Phone Number" />
                        </f:facet>
                        <h:outputText value="#{user.phone}" />
                    </p:column>
                    <p:column style="text-align: center;">
                        <f:facet name="header">
                            <h:outputText value="Email Address" />
                        </f:facet>
                        <h:outputText value="#{user.email}" />
                    </p:column>
                    <p:column style="text-align: center;">
                        <f:facet name="header">
                            <h:outputText value="DOB" />
                        </f:facet>
                        <h:outputText value="#{user.dob}">
                            <f:convertDateTime pattern="dd-MMM-yyyy" />
                        </h:outputText>
                    </p:column>
                    <p:column style="text-align: center;">
                        <f:facet name="header">
                            <h:outputText value="No. Memberships" />
                        </f:facet>
                        <h:outputText value="#{user.numberOfMemberships}" />
                    </p:column>
                    <p:column style="text-align: center;">
                        <f:facet name="header">
                            <h:outputText value="Actions" />
                        </f:facet>
                        <h:commandButton value="Update" action="#{uController.update}" />
                        <h:commandButton value="Delete" action="#{uController.delete}" />
                    </p:column>
                </p:dataTable>
                <h:panelGrid columns="2" cellpadding="2" id="footerPanelGrid"
                    border="0">
                    <h:commandButton action="#{uController.home}" value="Home Page" />
                </h:panelGrid>
            </p:panel>
        </h:form>
    </center>
</h:body>
</html>

However, neither of the buttons work. Instead they appear to simply refresh the view page. I have ran the app in debug and neither update or delete method is hit.

I suspect this may be due to using h:commandButton within a p:dataTable. However, I have also tried p:commandButton but to no avail.

For reference, here is a snippet of the UserController class:

@ManagedBean(name="uController")
public class UserController extends AbstractController {

    private Collection<User> users;

            ...

    public String update() {

        System.out.println("Ready for update");

        return "update-user";
    }

    public String delete() {

        System.out.println("Ready for delete");

        return "delete-user";
    }
            ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春如此纠结 2024-12-08 07:20:35

我找到了使用 ViewScoped bean 的解决方案。我为此遇到的文章:

学习 JSF2:托管 beans

我更改了我的h:commandButton to:

<h:commandButton value="Delete" action="#{userList.delete}">
    <f:setPropertyActionListener target="#{userList.selectedUser}"
        value="#{user}" />

我还重新设计了我的 UserController 类,它变成了 UserList:

import java.util.Collection;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

@ManagedBean(name="userList")
@ViewScoped
public class UserList {

    private Collection<User> users;
    private User selectedUser;

    @ManagedProperty(value="#{userService}")
    private UserService userService;

    public void setUserService(UserService userService) {

        this.userService = userService;
    }

    @PostConstruct
    public void populateList() {

        users = userService.getUsers();
    }

    public void delete() {

        // TODO remove from DB

        users.remove(selectedUser);
    }

    public User getSelectedUser() {

        return selectedUser;
    }

    public void setSelectedUser(User selectedUser) {

        this.selectedUser = selectedUser;
    }

    public Collection<User> getUsers() {

        return users;
    }

}

感谢您的回复 Zenzen。

I have found the solution using a ViewScoped bean. The article that I came across for this:

Learning JSF2: Managed beans

I changed my h:commandButton to:

<h:commandButton value="Delete" action="#{userList.delete}">
    <f:setPropertyActionListener target="#{userList.selectedUser}"
        value="#{user}" />

I also re-designed my UserController class and it became UserList:

import java.util.Collection;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

@ManagedBean(name="userList")
@ViewScoped
public class UserList {

    private Collection<User> users;
    private User selectedUser;

    @ManagedProperty(value="#{userService}")
    private UserService userService;

    public void setUserService(UserService userService) {

        this.userService = userService;
    }

    @PostConstruct
    public void populateList() {

        users = userService.getUsers();
    }

    public void delete() {

        // TODO remove from DB

        users.remove(selectedUser);
    }

    public User getSelectedUser() {

        return selectedUser;
    }

    public void setSelectedUser(User selectedUser) {

        this.selectedUser = selectedUser;
    }

    public Collection<User> getUsers() {

        return users;
    }

}

Thanks for your responses Zenzen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文