h:selectBooleanCheckBox对select的操作

时间:2015-07-01 12:28:44

标签: jsf jsf-2

我有一个<h:selectBooleanCheckBox>作为我的JSF的一部分,当它的状态从unchecked变为checked时,我想运行一个bean方法。

我有以下控制器bean

@Named
@ViewScoped
public class UserController {

   @Inject
   private UserService userService;

   @Inject
   private LocationService locationService;

   private UserFilter userFilter;

   private List<User> users;
   private List<Location> locations;

   @PostConstruct
   public void init() {
       users = userService.listAll();
       locations = locationService.listAll();
       userFilter = new UserFilter();
   }

   public List<User> getUsers() {
       return users;
   }

   public void setUsers(List<User> users) {
       this.users = users;
   }

   public List<Location> getLocations() {
       return locations;
   }

   public void setLocations(List<Location> locations) {
       this.locations = locations;
   }

   public void listAllUsers() {
       users = userService.listAll();
   }

   public void findUsers() {
      // code that uses the UserFilter
      // to decide which user filter find method to use
   }
}

UserFilter是一个简单的DTO

public class UserFilter {

    private boolean allUsers = true;

    private String username;
    private String location;

    //getters and setters
}

我的JSF就是这样

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Users</title>
    </h:head>
    <h:body>
        <h1>Users</h1>
        <h:form id="filterForm">
            <h:selectBooleanCheckbox id="selectAll" value="#{userController.userFilter.allUsers}" title="allUsers">
                <f:ajax render="filterGrid"/>
            </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 value="Filter" disabled="#{userController.userFilter.allUsers}" action="#{userController.findUsers()}"/>
            </h:panelGrid>
        </h:form>
        <h:form rendered="#{not empty userController.users}">
            <h:dataTable 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)}"/></h:column>
            </h:dataTable>
        </h:form>
        <h:panelGroup rendered="#{empty userController.users}">
            <p>Table is empty! Please add new items.</p>
        </h:panelGroup>
        <h3>Add user</h3>
        <h:form id="user">
            <p>Value: <h:inputText id="name" /></p>
            <p>
                <h:commandButton value="add" action="#{userController.add(param['user:name'])}"/>
            </p>
        </h:form>
    </h:body>
</html>

正如您在默认情况下所看到的,它会列出所有用户,然后在取消选中该复选框时,您可以选择过滤用户名/位置。

我想要的是复选框在状态从未选中状态移动到选中状态时运行userController.listAllUsers()方法。

还有一个小问题,如何让复选框显示在与面板网格项相同的行中?

1 个答案:

答案 0 :(得分:0)

我似乎习惯回答自己的问题!我需要一个额外的<f:ajax标记来呈现用户表单并设置了listener属性

类似

<h:selectBooleanCheckbox id="selectAll" value="#{userController.userFilter.allUsers}" title="allUsers">
    <f:ajax render="filterGrid"/>
    <f:ajax render="usersForm" listener="#{userController.listAllUsers()}"/>
</h:selectBooleanCheckbox><h:outputText value ="All users"/>