【问题标题】:h:selectBooleanCheckBox action on selecth:selectBooleanCheckBox 对选择的操作
【发布时间】:2015-07-01 12:28:44
【问题描述】:

我有一个 <h:selectBooleanCheckBox> 作为我的 JSF 的一部分,当它的状态从未选中变为选中时,我想运行一个 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() 方法。

还有一个小问题,如何让复选框与面板网格项出现在同一行?

【问题讨论】:

    标签: jsf jsf-2


    【解决方案1】:

    我似乎有回答自己问题的习惯!我需要一个额外的&lt;f:ajax 标记来呈现用户表单并设置监听器属性

    类似

    <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"/>
    

    【讨论】:

    • 只有一个效率更高。 render 属性仅支持以空格分隔的客户端 ID 集合。
    猜你喜欢
    • 2013-12-02
    • 2011-11-08
    • 1970-01-01
    • 2019-12-06
    • 2017-05-25
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多