共享bean
到此为止,对于jsp:useBean创建的对象,我们都将它们看作是_jspService方法中的局部变量来处理(jspService由页面生成的servlet的service方法调用)。虽然bean的确绑定到
局部变量,但这并非惟一的行为.它们还存储在4个不同的位置—依jsp:useBean的可选属性scope的值而定。
使用scope时,系统首先检查指定的位置是否存在指定名称的bean。仅当系统找不到现有的bean时,才会创建新的bean。这种行为使得servlet可以用下面的方式处理复杂的用户请求:建立bean,将它们存储在3个标准共享位置中的某个位置(请求、会话或servlet的上下文),然后将清求转发到几个适当JSP页面中的某个页面,提供适合于请求数据的结果。
如下所述,scope属性有4个可选值:Page(默认),request, session和 application.
.<jsp:useBear ...scope="page"/>
这是默认的值:完全省略scope属性也会得到相同的行为。page作用域表示:在处理当前请求期间,除了要将bean对象绑定到局部变量外,还应该将它放在PageContext对象中。将对象存储在此处表示,servlet可以通过调用预定义变量pageContext的getAttribute方法访它。由于每个页面和每个请求都有不同的PageContext对象,所以scope=page(或省略scope时表示不共享}bean,也就是针对每个请求都创建新的bean.
同理,其他三个属性值也可以类似理解。
request共享:bean
package cn.donghaua.bean;
public class BakedBean {
private String level="half-baked";
private String goesWith = "hot dog";
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public String getGoesWith() {
return goesWith;
}
public void setGoesWith(String goesWith) {
this.goesWith = goesWith;
}
}
主页面Baked.jsp
<body>
<H1>first requst page</H1>
<jsp:useBean id="requestBean" class="cn.donghaua.bean.BakedBean" scope="request"/>
<!-- <jsp:setProperty property="*" name="requestBean"/> -->
first page bakedLevel:
<jsp:getProperty property="level" name="requestBean"/><br>
first page goesWith:
<jsp:getProperty property="goesWith" name="requestBean"/><br>
<%= request.getRemoteHost() %><br>
<jsp:include page="/Baked_In.jsp"/>
</body>
被包含页面Baked_In.jsp;
this is the included page:<br>
<jsp:useBean id="requestBean" class="cn.donghaua.bean.BakedBean" scope="request"/>
<i>include level:</i>
<jsp:getProperty property="level" name="requestBean"/><br>
<i>include goesWith:</i>
<jsp:getProperty property="goesWith" name="requestBean"/>
输出:
first requst page
first page bakedLevel: half-baked
first page goesWith: hot dog
127.0.0.1
this is the included page:
include level: half-baked
include goesWith: hot dog
同理session及其他的属性也可以共享bean
共享bean的更多相关文章
- Spring配置文件解析--bean属性
1.bean设置别名,多个别名用逗号隔开 <!--使用alias--> <bean id="app:dataSource" class="...&quo ...
- bean 的各个属性
http://www.springframework.org/schema/beans/spring-beans.xsd org.springframework.beans.factory.confi ...
- 单例Bean注册表接口SingletonBeanRegistry
Github: SingletonBeanRegistry.java SingletonBeanRegistry package org.springframework.beans.factory.c ...
- 浅析Spring中bean的作用域
一.前言 刚刚花了点时间,阅读了一下Spring官方文档中,关于bean的作用域这一块的内容.Spring-4.3.21官方文档中,共介绍了七种bean作用域,这篇博客就来简单介绍一下这七种作用域 ...
- 【Spring】IoC容器 - Spring Bean作用域Scope(含SpringCloud中的RefreshScope )
前言 上一章学习了[依赖来源],本章主要讨论SpringBean的作用域,我们这里讨论的Bean的作用域,很大程度都是默认只讨论依赖来源为[Spring BeanDefinition]的作用域,因为在 ...
- 面试突击79:Bean 作用域是啥?它有几种类型?
Spring 框架作为一个管理 Bean 的 IoC 容器,那么 Bean 自然是 Spring 中的重要资源了,那 Bean 的作用域是什么意思?又有几种类型呢?接下来我们一起来看. PS:Java ...
- spring框架学习(三)
一.Spring自动组件扫描 Spring 提供组件扫描(component scanning)功能.它能从指定的classpath里自动扫描.侦测和实例化具有特定注解的组件. 基本的注解是@Comp ...
- springMvc源码学习之:spring源码总结
转载自:http://www.cnblogs.com/davidwang456/p/4213652.html spring beans下面有如下源文件包: org.springframework.be ...
- jdk 1.5新特性说明
“JDK1.5”的一个重要主题就是通过新增一些特性来简化开发,这些特性包括泛型,for-each循环,自动装包/拆包,枚举,可变参数,静态导入.使用这些特性有助于我们编写更加清晰,精悍,安全的代码. ...
随机推荐
- webpack入门笔记
此为第一篇主要是webpack入门笔记: http://if-true.com/2015/10/16/webpack-tutorial-translate.html
- CSU - 1356 Catch(dfs染色两种写法,和hdu4751比较)
Description A thief is running away! We can consider the city to N–. The tricky thief starts his esc ...
- Bone Collector(01背包+记忆化搜索)
Bone Collector Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
- 怎样在小方框上打对号 小方框内打对勾 word 方框打对勾
在word中做选择时,非常多人遇到须要在小方框上打对勾而不知怎样做,现将可行的各种方法总结例如以下: 1:直接找到一个做好的,保存为图片,在须要的时候插入它: 2:插入文本框,然后边框选择为实线,在文 ...
- .net dropdownlist 動態顯示,指定字段
dropdownlist 動態顯示,指定字段 this.ddlPermission.Items.Clear(); ,new ListItem("請選擇","") ...
- 使用sqlcmd执行连接的时候一直报有语法错误
1.今天在使用sqlcmd进行执行连接操作的时候一直报有语法错误找了半天. 命令 sqlcmd -S 服务器名称 -U 帐户 -P 密码 示例 sqlcmd -S "LOCALHOST&qu ...
- D3学习教程
[ D3.js 入门系列 ] 入门总结 | OUR D3.JS http://www.ourd3js.com/wordpress/?p=396
- 新浪云-PHP实现上传原图,缩略图
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < ...
- SQL Server表和字段说明的增加和更新
1. 增加字段说明 EXEC sp_addextendedproperty 'MS_Description', 'some description', 'user', ...
- inline-block代替浮动布局float:left列表布局最佳方案
基于各位前辈的辛勤劳动,下面得出使用inline-block替换float:left;的最佳方案. html代码 <div class="list"> <ul&g ...