Why JSF2 @ViewScoped not working?

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值