在h:commandButton操作上更新表单

时间:2015-07-02 07:56:36

标签: jsf jsf-2

我在h:dataTable中每行都有一个删除记录的按钮。该操作在删除记录时起作用。但表格没有更新,我不确定是什么错?这是JSF

    <h:form id="usersForm" rendered="#{not empty userController.users}">
        <h:dataTable id="usersTable" value="#{userController.users}" var="user">
            <h:column>#{user.name}</h:column>
            <h:column>#{user.location.location}</h:column>
            <h:column>
                <h:commandButton value="delete" action="#{userController.delete(user)}">
                    <f:ajax render="usersForm usersTable"/>
                </h:commandButton>
            </h:column>
        </h:dataTable>
    </h:form>

编辑:我不相信指向的答案是完全相同的,在看了答案之后我发现生成的html将表定义为<table id="usersForm:usersTable">所以我修改了JSF如下

    <h:form id="usersForm" rendered="#{not empty userController.users}">
        <h:dataTable id="usersTable" value="#{userController.users}" var="user">
            <h:column>#{user.name}</h:column>
            <h:column>#{user.location.location}</h:column>
            <h:column>
                <h:commandButton value="delete" action="#{userController.delete(user)}">
                    <f:ajax render=":usersForm:usersTable"/>
                </h:commandButton>
            </h:column>
        </h:dataTable>
    </h:form>

它仍然没有更新。在表单之外的一些额外细节我有另一个过滤器形式,它在执行时更新表,这里是完整的JSF

    <h:form id="filterForm">
        <h:selectBooleanCheckbox id="selectAll" value="#{userController.userFilter.allUsers}" title="allUsers">
            <f:ajax render="filterGrid usersForm" listener="#{userController.listAllUsers}"/>
        </h:selectBooleanCheckbox><h:outputText value ="All users"/>
        <h:panelGrid id="filterGrid" columns="3">
            <h:inputText id="userName" value="#{userController.userFilter.userName}" disabled="#{userController.userFilter.allUsers}"/>
            <h:selectOneMenu id="selectLocation" value="#{userController.userFilter.location}" disabled="#{userController.userFilter.allUsers}">
                <f:selectItems value="#{userController.locations}" var="location" itemValue="#{location.location}" itemLabel="#{location.location}"/>
            </h:selectOneMenu>
            <h:commandButton id="filterButton" value="Filter" disabled="#{userController.userFilter.allUsers}" action="#{userController.findUsers()}"/>
        </h:panelGrid>
    </h:form>
    <h:form id="usersForm" rendered="#{not empty userController.users}">
        <h:dataTable id="usersTable" value="#{userController.users}" var="user">
            <h:column>#{user.name}</h:column>
            <h:column>#{user.location.location}</h:column>
            <h:column>
                <h:commandButton value="delete" action="#{userController.delete(user)}">
                    <f:ajax render=":usersForm:usersTable"/>
                </h:commandButton>
            </h:column>
        </h:dataTable>
    </h:form>

和控制器bean

@Named
@ViewScoped
public class UserController implements Serializable
{

    @Inject
    private UserService userService;    

    private List<User> users;    

    private User user;

    /**
     * Init method used to initialise users list
     */
    @PostConstruct
    public void init()
    {
        users = userService.listAll();
    }

    /**
     * Delete the specified user
     *
     * @param user User to be deleted
     */
    public void delete(User user)
    {
        userService.deleteUser(user);
    }        

}

服务

@Stateless
public class UserService
{

    @PersistenceContext(unitName = "UsersJSFApplicationPU")
    private EntityManager em;

    /**
     * Deletes the specified user from the database
     *
     * @param user to delete
     */
    public void deleteUser(User user)
    {
        User usr = em.find(User.class, user.getId());
        em.remove(usr);
    }

    /**
     * Returns a list of all users
     *
     * @return user list
     */
    public List<User> listAll()
    {
        return em.createQuery("SELECT u from User as u").getResultList();
    }

}

1 个答案:

答案 0 :(得分:0)

哎呀学校男孩错误,控制器删除方法错误,需要

/**
 * Delete the specified user
 *
 * @param user User to be deleted
 */
public void delete(User user)
{
    userService.deleteUser(user);
    users = userService.listAll();
}