问题
Here is my code,
search.xhtml page
<h:form id="searchform">
<h:inputText id="deptId" value="#{searchBean.departmentId}"></h:inputText>
<h:inputText id="deptName" value="#{searchBean.deparmentName}"></h:inputText>
<h:commandButton value="Click to Search" action="#{searchBean.searchEmployees}">
</h:commandButton>
</h:form>
searchresults.xhtml page
<rich:dataTable value="#{searchBean.employeeList}" var="e" id="table">
<rich:column>
<f:facet name="header">
<h:outputText value="FNAME"/>
</f:facet>
<h:outputText value="#{e.fName}"/>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="LNAME"/>
</f:facet>
<h:outputText value="#{e.lName}"/>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="DEPARTMENT"/>
</f:facet>
<h:outputText value="#{e.dept}"/>
</rich:column>
</rich:dataTable>
In the managed bean
ManagedBean
@ManagedBean(name="searchBean")
@RequestScoped
public class SearchBean implements Serializable{
private String departmentId;
private String deparmentName;
private List<EmpBean> employeeList;
//....get/set 's
public String searchEmployees(){
employeeList = service.getEmployees(departmentId,deparmentName);
return "searchresults.xhtml";
}
Issue: searchresults page is is not displaying records though it fetched records from table I'm able to achieve this using search bean as session scope but i want to use the scope as Requestscope, bec of performance. Please suggest...!
回答1:
The Problem
The reason you're not seeing the results is because you have an incorrect understanding of the @RequestScope
and consequently, improbable expectations of the scope
searchEmployees
executes during the INVOKE_APPLICATION
phase, the second to the last phase of the JSF request processing lifecycle. By the time the user is redirected to searchResults.xhtml, the instance of your SearchBean
that holds the search results has been destroyed and a brand new one created, resulting in an empty list.
This is the rule for every bean scope except the @SessionScoped
: a navigation action will cause the old bean to be destroyed and a new one created. This is not to say that @SessionScoped
should be your scope of choice (it's generally a terrible idea to back a page with a @SessionScoped
bean).
The Solution
Use the FlashScope to temporarily stash the results just for display on the other page. For example
employeeList = service.getEmployees(departmentId,deparmentName);
Flash theFlashScope = FacesContext.getCurrentInstance().getExternalContext().getFlash();
theFlashScope.put("searchResults",employeeList);
return "searchresults.xhtml";
Then in searchResults.xhtml
<rich:dataTable value="#{flash.searchResults}" var="e" id="table">
Here, #{flash}
is an implicit EL object; you don't have to do anything else.
EDIT: Based on your "answer", you should be aware that the Flash
object keeps variables stored in it for only the first render of the page. Subsequent HTTP requests will clear the content of the flash object.
If you're interested in keeping the content of the flash object beyond the first render, you should use the Flash#keep
:
<rich:dataTable value="#{flash.keep.searchResults}" var="e" id="table">
Further Reading
- Max Katz's blog on the Flash object
来源:https://stackoverflow.com/questions/23307336/jsf2-search-bean-request-scope-is-not-displaying-results