今天有两个问题,
我有这样的代码和平:
目录.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
<h:head>
<f:facet name="first">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</f:facet>
<title>booXtore - Catalog</title>
<h:outputStylesheet library="css" name="main.css"/>
<h:outputStylesheet library="css" name="font-awesome.css"/>
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</h:head>
<h:body>
<ui:include src="/WEB-INF/includes/navbar.xhtml"/>
<div id="wrap">
<div id="main" class="container clear-top">
<ol class="breadcrumb">
<li class="active">Catalogue</li>
</ol>
<h:form >
<div class="row">
<div class="col-md-6">
<p:inputText id="searchBar" type="search" styleClass="form-control" placeholder="Recherche" value="#{searchProviderBean.search}"/>
</div>
<div class="col-md-4">
<h:selectOneMenu styleClass="form-control" value="#{searchProviderBean.searchCategory}">
<f:selectItem itemValue="#{searchProviderBean.defaultCategorySearch}"
itemLabel="#{searchProviderBean.defaultCategorySearch.name}"/>
<c:forEach items="#{searchProviderBean.categories}" var="category">
<f:selectItem itemValue="#{category}"
itemLabel="#{category.name}"/>
</c:forEach>
</h:selectOneMenu>
</div>
<div class="col-md-2">
<h:commandLink class="btn btn-info" value="Rechercher" action="#{searchProviderBean.launchSearch}"/>
</div>
</div>
</h:form>
</div>
</div>
</h:body>
</hthml
SearchProviderBean:
package com.booXtore;
import com.booXtore.domain.Books;
import com.booXtore.domain.Categories;
import com.booXtore.service.BooksFacadeLocal;
import com.booXtore.service.CategoriesFacadeLocal;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
@ManagedBean
@ViewScoped
public class SearchProviderBean implements Serializable {
@EJB
private BooksFacadeLocal bFL;
@EJB
private CategoriesFacadeLocal cFL;
private String search;
private Categories searchCategory;
/**
* Creates a new instance of SearchProviderBean
*/
public SearchProviderBean() {
}
public List<Categories> getCategories() {
List<Categories> result = cFL.findAll();
return result;
}
public Categories getSearchCategory() {
FacesContext fc = FacesContext.getCurrentInstance();
for(Categories cat : this.cFL.findAll())
{
if(cat.getName().equalsIgnoreCase(getParam(fc, "category")))
{
this.searchCategory = cat;
}
}
return this.searchCategory;
}
public void setSearchCategory(Categories searchCategory) {
this.searchCategory = searchCategory;
}
public String launchSearch() {
String cat = "";
if(!this.searchCategory.getName().equalsIgnoreCase("Toutes Catégories"))
{
cat = "category=" + this.searchCategory.getName();
}
return "/catalog.xhtml?faces-redirect=true&search=" + search + cat;
}
public String getSearch() {
FacesContext fc = FacesContext.getCurrentInstance();
String searchParam = getParam(fc , "search");
if(searchParam != null)
{
this.search = searchParam;
}
return search;
}
public void setSearch(String search) {
this.search = search;
}
public Categories getDefaultCategorySearch()
{
Categories def = new Categories();
def.setName("Toutes Catégories");
return def;
}
public List<Books> getSearchResults() {
return null;
}
public List<Books> getAllBooks()
{
return this.bFL.findAll();
}
private String getParam(FacesContext fc, String paramName)
{
return fc.getExternalContext().getRequestParameterMap().get(paramName);
}
}
然后是问题:
方法
setSearch(String search)
和setSearchCategory(Categories searchCategory)
永远不会被调用(通过断点检查)commandLink 操作方法永远不会被调用(通过 return 语句上的断点检查),只会重新加载页面。
我尝试将 commandLink 更改为 commandButton :除了样式更改之外没有任何效果我尝试将@ViewScoped
注释更改为@SessionScoped
但没有效果
编辑:
由于整个页面的大小,并且因为我对 jsf 很陌生,我没有看到页面底部显示的错误消息,它告诉我在转换com.booXtore.domain.Category[ id=X ]
for null 转换器的值时出错