jsf2搜索bean请求范围未显示结果 [英] jsf2 search bean request scope is not displaying results

查看:135
本文介绍了jsf2搜索bean请求范围未显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,

search.xhtml页面

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页面

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>

在托管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";
    }

问题:尽管从表中获取了记录,但searchresults页面未显示记录 我可以使用搜索bean作为会话作用域来实现此目的,但我想将该作用域用作Requestscope,因为性能如此. 请建议...!

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...!

推荐答案

问题

之所以看不到结果,是因为您对@RequestScope的理解不正确,因此对范围的期望不高

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在JSF请求处理生命周期的第二个到最后一个阶段的INVOKE_APPLICATION阶段执行.到将用户重定向到 searchResults.xhtml 时,保存搜索结果的SearchBean实例已被破坏,并创建了一个全新的实例,从而导致列表为空.

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.

这是除@SessionScoped之外的每个bean范围的规则:导航操作将导致旧bean被销毁并创建一个新bean.这并不是说@SessionScoped应该是您的选择范围(使用@SessionScoped bean支持页面通常是一个糟糕的主意).

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).

解决方案

使用 FlashScope 暂时隐藏结果,仅在其他页面上显示.例如

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

然后在 searchResults.xhtml

<rich:dataTable value="#{flash.searchResults}" var="e" id="table">

在这里,#{flash}是隐式的EL对象;您无需执行其他任何操作.

Here, #{flash} is an implicit EL object; you don't have to do anything else.

编辑:基于您的答案",您应该意识到Flash对象仅在页面的第一次呈现时将变量存储在其中.随后的HTTP请求将清除Flash对象的内容.

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.

如果您有兴趣将Flash对象的内容保留在第一个渲染之外,则应使用Flash#keep:

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

进一步阅读

这篇关于jsf2搜索bean请求范围未显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆