更新h:commandButton动作的表单 [英] Update form on h:commandButton action

查看:42
本文介绍了更新h:commandButton动作的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a button per row in a h:dataTable that deletes the record. The action works as it deletes the record. But the table doesn't update, I'm not sure what's wrong? Here's the 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

I don't believe the answer pointed to is quite the same, after look at the answer I found the generated html had the table defined as <table id="usersForm:usersTable"> So I modified the JSF as follows

    <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

It still doesn't update. A bit of additional detail outside of the form I have another filter form that does update the table when it is actioned, here is the complete 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();
    }

}

推荐答案

Argh小学生错误,控制器的删除方法错误,需要

Argh school boy error, the controller delete method was wrong, needs to be

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

这篇关于更新h:commandButton动作的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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