`

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
分享到:
评论

相关推荐

    AulaJava-ExercicioCRUDAlunos:关于使用@ViewScoped创建简单的Crud的练习存储库

    `@ViewScoped`是JavaServer Faces(JSF)框架中的一个注解,用于管理bean的作用域,它在处理这些操作时扮演着重要角色。本项目"AulaJava-ExercicioCRUDAlunos"就是一个使用@ViewScoped进行简单CRUD操作的实践示例。 ...

    JSF2新特性以及配置

    5. **Managed Bean注解支持**:JSF2支持使用`@ManagedBean`和`@ViewScoped`等注解来声明和管理Bean,简化了代码并增强了可读性。 6. **CDI集成**:JSF2与Java EE的Contexts and Dependency Injection (CDI)框架集成...

    jsf 的Annotations配置

    2. `@SessionScoped`、`@ApplicationScoped`、`@ViewScoped`和`@RequestScoped`:这些注解定义了Bean的作用域。`@SessionScoped`表示Bean在用户会话期间有效,`@ApplicationScoped`在整个应用程序中只有一个实例,`@...

    JSF2 规范的标准参考实现

    5. **注解驱动**:JSF 2大量引入了注解,如`@ManagedBean`、`@ViewScoped`和`@Inject`,使得配置更加简洁,降低了XML配置文件的依赖。 6. **CDI集成**:JSF 2与Contexts and Dependency Injection (CDI)框架紧密...

    一个简单的jsf例子------JSF2学习笔记1

    在JSF2中,可以通过注解如`@ManagedBean` 和 `@ViewScoped` 来轻松创建和配置bean。例如,我们可以定义一个名为`UserBean` 的bean,用于处理用户的登录信息。 4. **EL表达式** Expression Language (EL) 是JSF中的...

    基于JSF全注解框架

    `@ManagedBean`注解标记一个类作为JSF管理Bean,而`@ViewScoped`则定义了Bean的作用范围,使得Bean的生命周期与特定视图(页面)关联。此外,还可以使用`@FacesConverter`和`@FacesValidator`注解来自定义转换器和...

    JSF2中Navigation的配置

    在JSF2中,我们可以通过在`@ManagedBean`注解上使用`@ViewScoped`或`@SessionScoped`等来控制bean的生命周期,进而影响导航。例如,如果一个bean是视图作用域,那么当导航发生时,该bean会被销毁;如果是会话作用域...

    JSF入门+JSF web实战+JSF2

    《JSF2》是关于JSF的第二版,相比第一版,JSF2引入了许多新特性,如Facelets作为默认视图层技术,使得视图更易于维护;引入了Partial State Saving,降低了服务器内存消耗;支持CDI(Contexts and Dependency ...

    jsf2

    **JSF 2.0(JavaServer Faces 2.0)详解** JavaServer Faces (JSF) 是一种用于构建Web应用程序的Java EE框架,它专注于视图层的开发。JSF 2.0是该框架的一个重要版本更新,带来了许多改进和新特性,极大地提升了...

    JSF分页组件2

    **JSF分页组件2详解** JavaServer Faces (JSF) 是Java平台上的一个用于构建Web应用程序的MVC(Model-View-Controller)框架。在处理大量数据时,分页功能是必不可少的,它能够帮助用户更有效地浏览和管理信息。在...

    JSF2-Getting-Started.zip_Getting Started

    **JSF2 入门指南** JavaServer Faces (JSF) 是一个用于构建Web应用程序的MVC(模型-视图-控制器)框架,由Java Community Process(JCP)开发并维护。JSF 2.0是该框架的一个重要版本,引入了许多改进和新特性,使得...

    jsf资料

    《JSF2和RichFaces4使用指南》这本书可能涵盖了如何配置JSF环境,创建和使用Managed Beans,理解JSF生命周期,以及如何利用RichFaces 4的组件进行高效开发。通过阅读这本书,开发者可以深入理解JSF2和RichFaces4的...

    JSF学习,JSF标签使用

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

    JSF API帮助文档

    这些bean可以被JSF组件直接引用,通过`@ManagedBean`和`@ViewScoped`等注解进行管理。 **EL(Expression Language)** 是JSF的一部分,它是一种强大的表达式语言,用于在视图层获取和设置Managed Bean的属性。EL...

    Spring-Mybatis-JSF

    JSF的`@ManagedBean`注解用于标记一个类为JSF管理的Bean,而`@ViewScoped`或`@RequestScoped`则定义Bean的作用域。在JSF页面中,可以通过EL(Expression Language)和Primefaces组件来访问和展示数据。 例如,一个...

    JSF2.0基本环境

    - Managed Beans是JSF中的业务逻辑组件,可以使用注解定义bean及其作用域,如`@ManagedBean(name="helloBean")`和`@ViewScoped`。 - 通过EL(Expression Language)在视图和bean之间传递数据,如`#{helloBean....

    jsf 文件上传和下载

    在JavaServer Faces (JSF)框架中,文件上传和下载是常见的功能,广泛应用于Web应用程序中,例如用户需要上传个人照片、下载文档等。本文将详细介绍如何在JSF环境中实现文件上传和下载,并提供一个可直接在Eclipse下...

    jsf 规范编写文当

    **JSF(JavaServer Faces)规范详解** JSF(JavaServer Faces)是Java平台上用于构建Web应用程序的一种组件模型。它提供了丰富的用户界面组件、事件处理和数据管理功能,旨在简化开发人员的工作流程,提高Web应用的...

    JSF 2 + Spring 3 integration example

    **JSF 2 + Spring 3 集成示例** JavaServer Faces (JSF) 是一个用于构建 Web 应用程序的 Java EE 框架,它专注于视图层的构建,而 Spring 框架则是一个全面的后端开发框架,包括依赖注入、事务管理、AOP(面向切面...

Global site tag (gtag.js) - Google Analytics