`

Why JSF2 @ViewScoped not working?

    博客分类:
  • JEE
阅读更多

javax.faces.bean.ViewScoped said as long as you stay on the same view/page, the backing bean should be the same. This is useful if you want to retain some state of the backing bean, whilst avoid caching too much stuff in the session. I think it's quite useful in the searching/pagination screens.

 

However, my previous testing showed that it's not as expected in that it created new instances for different request for the searchStudent screen.

 

A recent reading solved this myth.

 

The problem is that although the search method "findByName" returned the same page "student/studentSearch.xhtml", JSF treated it as different view. To make it "stays on the same view", the search method in the backing bean should return null or nothing. Here's how it should look like:

@ManagedBean(name="ss")
@ViewScoped
public class StudentSearch implements Serializable {

  ...
  public void findByName() {
    log.debug("searching student for name: " + this.nameFilter);
    searchResultList = dao.find(this.nameFilter, maxRows);
  }

  or

  public String findByName() {
    log.debug("searching student for name: " + this.nameFilter);
    searchResultList = dao.find(this.nameFilter, maxRows);
    return null;
  }
  ...

}

If we code the search method like this, JSF would think it stays on the same view and javax.faces.bean.ViewScoped should be working as expected. This would enbable you replace the @SessionScoped backing bean, if you are concerned about caching too many objects in session.

 

This is working, but if you take a look at the server log, JSF2 actually still creates a new instance for you if you click column header to sort, or a page number to goto etc. But it manges to maintain the state for you, transparently.

 

If you really want to stick to the "same" instance, it looks like that you can configure one of these JSF properties in "web.xml". Use of "javax.faces.FULL_STATE_SAVING_VIEW_IDS" is recommended and you can define a comma separated list of pages/view for it.

<!-- it's said that this would incur performance cost
<context-param>
    <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
    <param-value>false</param-value>
</context-param>
-->
	 
<context-param>
    <param-name>javax.faces.FULL_STATE_SAVING_VIEW_IDS</param-name>
    <param-value>/student/studentSearch.xhtml,/student/studentDetails.xhtml</param-value>
</context-param>

 

So lets sum it up here:

A @ViewScoped bean will live as long as you're submitting the form to the same view again and again. In other words, as long as when the action method(s) returns null or even void, the bean will be there in the next request. Once you navigate to a different view, then the bean will be trashed.

-- from: http://balusc.blogspot.co.nz/2010/06/benefits-and-pitfalls-of-viewscoped.html
分享到:
评论

相关推荐

    JSF常见注解

    JSF常用注解,常见的JSF注解的使用方法及其解释。

    JSF学习,JSF标签使用

    JSF的学习入门知识教程,里面有例子还有各个标签的使用及属性介绍

    JSF入门+JSF web实战+JSF2

    JSF入门详细资料(PDF),包括JSF入门中文版,JSF Web应用实战开发,以及JSF2,JSF入门必配学习资料

    JSF2新特性以及配置

    JSF2 richfaces 中文文档

    jsf2segy.zip

    浅剖数据jsf格式数据转换为segy数据,可以实现数据转换,以至于在其他软件上处理数据

    Beginning JSF 2 APIs and JBoss Seam

    Beginning JSF 2 APIs and JBoss? Seam You’ll learn to create and enhance an eShop using practical methods, and can repurpose the template for your own personal and professional projects. What you’...

    JSF2datatable分页控件与左侧菜单

    JSF2.xdatatable分页控件与左侧菜单最简单应用  JSF2.x,功能强大,使用方便。全世界使用JSF的人越来越多。而且也有很多很好的控件给发出来了,如:richfaces、primefaces、myfaces等,就日前来讲primefaces功能...

    jsf实例jsf实例 JSF学习 JSF jar包 JSF

    jsf实例 JSF学习 JSF jar包 JSF jsf实例 JSF学习 JSF jar包 JSFjsf实例 JSF学习 JSF jar包 JSF jsf实例 JSF学习 JSF jar包 JSF

    pdfhub-jsf:基于 JSF 2 的 Web 应用程序,可从 github wiki 生成 PDF

    将任何 Github wiki 转换为 PDF! 此应用程序面向需要以跨... 在 OpenShift 上的 JBoss EAP 卡盒中使用 JSF 2/Facelets 实现。 该应用程序具有轻量级前端,不使用持久层、@ViewScoped CDI bean 和促进 PDF 创建的库。

    JSF2和RICHFACES4使用指南

    JSF2和RICHFACES4使用指南

    petstore:一个解释 JSF 特性使用的启动示例项目

    该项目试图涵盖 JSF 2 规范的主要功能。 使用的字段是: 托管 bean,在它们不同的范围内(@ApplicationScoped、@SessionScoped、@ViewScoped、@RequestScoped) 在 JSF 中从视图到视图的导航 EL 语言在其 2.2 版本...

    Beginning JSF2 APIs and JBoss Seam

    jboss seam 和jsf2 开发好书

    JSF分页组件2

    JSF分页组件2,JSF分页组件2

    jsf分页 jsf分页 jsf分页

    jsf 分页 jsf 分页 jsf 分页 jsf 分页

    JSF Java Server Faces (JSF)框架

     2、Java Server Faces (JSF) Java Server Faces (JSF)  JSF是一种用于构建Java Web 应用程序的标准框架(是Java Community Process 规定的JSR-127标准)。它提供了一种以组件为中心的用户界面(UI)构建方法,从而...

    jsf2+spring sample

    jsf2+spring sample

    JSF2.xdatatable分页控件与左侧菜单最简单应用

    JSF2.xdatatable分页控件与左侧菜单最简单应用  JSF2.x,功能强大,使用方便。全世界使用JSF的人越来越多。而且也有很多很好的控件给发出来了,如:richfaces、primefaces、myfaces等,就日前来讲primefaces功能...

    JSF2和RICHFASES4使用指南

    JSF2和RICHFASES4使用指南,JSF(Java Server Faces)是JCP标准化组织通过的Web应用开发标准框架,RichFaces是RedHat公司的JBoss社区开发的一套基于JSF的UI组件库,支持异步请求功能

    《JSF_实战》非常好的JSF学习书

    《JSF_实战》非常好的JSF学习书《JSF_实战》非常好的JSF学习书《JSF_实战》非常好的JSF学习书《JSF_实战》非常好的JSF学习书《JSF_实战》非常好的JSF学习书《JSF_实战》非常好的JSF学习书《JSF_实战》非常好的JSF...

    JSF2学习用范例

    该范例为JSF2基础范例工程,分10类.适合初学者学习。 在weblogic9 tomcat5-7 JDK1.5测试通过 weblogic9和tomcat5需要el.jar支持,已经在文件夹中标明,只需放入lib目录下即可 需要注意的是weblogic9和tomcat5支持的...

Global site tag (gtag.js) - Google Analytics