jsf2 commandButton,commandLink不会触发操作

时间:2013-01-17 09:13:06

标签: jsf-2 commandbutton

在此页面中,h:commandButton不会触发托管bean中的操作方法,但是当我单击按钮时,它会重新加载当前页面。我不知道为什么请帮忙。

    <?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition 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://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      >
  <h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Bootstrap 101 Template</title>
    <!-- Bootstrap -->
    <h:outputStylesheet library ="css" name="bootstrap.css"/>
    <h:outputStylesheet library ="css" name="bootstrap-responsive.css"/>
    <h:outputScript library="js" name="bootstrap.js"/>
    <h:outputScript library="js" name="bootstrap-responsive.js"/>
  </h:head>
  <h:body>
      <h:form>           
                              <label><h3>Search</h3></label>


                              <h:selectOneMenu id="searchtype" value="#{searchView.searchType}" style="width: 230px">
                                  <f:selectItems value="#{searchView.searchType}"/>

                              </h:selectOneMenu>
                              <br/>
                              <h:inputText id="searchvalue" value="#{searchView.searchvalue}" required="true" style="height: 30px; width: 230px">
                                  <p:watermark value="Search type" for="searchvalue"/>
                              </h:inputText>
                              <br/>

                              <h:commandButton id="searchbtn" value="Search" action="#{searchView.prepareSearchResultView()}" styleClass="btn"/>

      </h:form>
  </h:body>
</html>

我的searchView托管bean是会话范围的,这里是托管bean中的prepareSearchResultView()方法;

    public String prepareSearchResultView(){

    this.searchResultList = searchCustomer();
    if(!this.searchResultList.isEmpty()){
        this.citizenSearchResultList = getCitizenSearchList();
        this.orgSearchResultList = getOrganizationSearchList();
        if(getCitizenSearchList().getRowCount() >0){
            return "citizensearchlist";
        }else if(getOrganizationSearchList().getRowCount()>0){
            return "orgsearchlist";
        }

    }else
        return "nosearchresult";
    //FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "no such result", "no such result"));
    return null;
}

和getSearchType()方法

    public Map<String, Object> getSearchType(){
    Map<String, Object> returnMap = new LinkedHashMap<String, Object>();
    if(loginview.isAbleToGetCitizenInfo() || loginview.isUserAdmin()){
        returnMap.put("searchtype1", "searchcitizenbyregno");
        returnMap.put("searchtype2", "searchcitizenbyname");
    }
    if(loginview.isAbleToGetOrgInfo() || loginview.isUserAdmin()){
        returnMap.put("searchtype3", "searchorgbyregno");
        returnMap.put("searchtype4", "searchgorgbyname");
        returnMap.put("searchtype5", "searchorgbystateregno");
    }
    return returnMap;
}

1 个答案:

答案 0 :(得分:2)

您以错误的方式使用<h:selectOneMenu>。您在selectMenu中的值是一个地图,您无法在不使用转换器的情况下从选择菜单中设置一种地图类型的值。在选择项中,您应该将value属性绑定到托管bean中的某个字符串。然后在actionMethod中,您可以使用此选定类型从地图中检索所选值。

<h:selectOneMenu id="searchtype" value="#{searchView.selectedType}" style="width: 230px">
    <f:selectItems value="{searchView.searchType}"/>
</h:selectOneMenu>

请查看以下帖子以更好地理解这个概念:Primefaces selectOneMenu listener not called with Objects other than Strings

通过具有业务逻辑的getter检索f:selectItems值也不是一个好习惯。您应该在托管bean中创建一个属性,在构造函数中初始化该映射。除了使用地图之外,最好使用列表并将密钥和值封装在自定义对象中。

<h:selectOneMenu id="searchtype" value="#{searchView.selectedType}" style="width: 230px">
    <f:selectItems value="{searchView.searchType}" 
                   var="searchType" 
                   itemLabel=#{searchType.key} 
                   itemValue=#{searchType.value}/>
</h:selectOneMenu>

List<RowItems> searchType = Lists.newArrayList(); // guave constructor

@PostConstruct
public void initBean(){
    if(loginview.isAbleToGetCitizenInfo() || loginview.isUserAdmin()){
        searchType.add(new RowItems("searchType1", "searchcitizenbyregno"));
        searchType.add(new RowItems("searchtype2", "searchcitizenbyname"));
    }
    if(loginview.isAbleToGetOrgInfo() || loginview.isUserAdmin()){
        searchType.add(new RowItems("searchtype3", "searchorgbyregno"));
        searchType.add(new RowItems("searchtype4", "searchgorgbyname"));
        searchType.add(new RowItems("searchtype5", "searchorgbystateregno"));
    }
}

public List<RowItems> getSearchType(){
    return searchType
}

最后,您的prepareSearchList函数的最后一个语句return null不是必需的,应根据您发布的内容删除,因为它是无法访问的代码。