问题描述
视图范围是什么意思?谁能解释一下,以便我了解它与请求范围有何不同?
What does the view scope mean? Can anyone explain about it, so that I can understand how it differs from the request scope?
推荐答案
@ViewScoped
bean的生存时间与JSF视图完全一样.它通常以全新的GET请求或导航操作开始,然后只要最终用户将视图中的任何POST表单提交至返回null
或void
的操作方法(然后向后导航),就将一直存在到相同视图).刷新页面或返回非null
字符串(甚至是空字符串!)的导航结果后,视图范围将结束.
A @ViewScoped
bean lives exactly as long as a JSF view. It usually starts with a fresh new GET request, or with a navigation action, and will then live as long as the enduser submits any POST form in the view to an action method which returns null
or void
(and thus navigates back to the same view). Once you refresh the page, or return a non-null
string (even an empty string!) navigation outcome, then the view scope will end.
一个@RequestScoped
bean的寿命与HTTP请求的寿命完全一样.因此,它将在每个请求结束时进行垃圾回收,并在每个新请求上重新创建它,从而丢失所有更改的属性.
A @RequestScoped
bean lives exactly as long a HTTP request. It will thus be garbaged by end of every request and recreated on every new request, hereby losing all changed properties.
因此,@ViewScoped
bean在启用Ajax的丰富视图中特别有用,该视图需要记住Ajax请求之间的(已更改)视图状态. @RequestScoped
一个将在每个Ajax请求上重新创建,因此无法记住所有更改的视图状态.请注意,像@SessionScoped
bean一样,@ViewScoped
bean在同一会话中的不同浏览器选项卡/窗口之间不共享任何数据.每个视图都有自己独特的@ViewScoped
bean.
A @ViewScoped
bean is thus particularly more useful in rich Ajax-enabled views which needs to remember the (changed) view state across Ajax requests. A @RequestScoped
one would be recreated on every Ajax request and thus fail to remember all changed view state. Note that a @ViewScoped
bean does not share any data among different browser tabs/windows in the same session like as a @SessionScoped
bean. Every view has its own unique @ViewScoped
bean.
- How to choose the right bean scope?
- The benefits and pitfalls of
@ViewScoped
这篇关于托管bean中的View和Request范围之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!