Update form on h:commandButton action

左心房为你撑大大i 提交于 2019-12-25 07:50:07

问题


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>

Edit: 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>

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>

And the controller 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);
    }        

}

And the service

@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:


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();
}        


来源:https://stackoverflow.com/questions/31178676/update-form-on-hcommandbutton-action

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!