托管 bean 中的 View 和 Request 范围之间的区别 [英] Difference between View and Request scope in managed beans

查看:24
本文介绍了托管 bean 中的 View 和 Request 范围之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

视图范围是什么意思?谁能解释一下,以便我了解它与请求范围的区别?

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 请求上重新创建,因此无法记住所有更改的视图状态.请注意,@ViewScoped bean 不会像 @SessionScoped 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.

这篇关于托管 bean 中的 View 和 Request 范围之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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