bean系列标签讲解
首页
<bean:header> <bean:parameter> <bean:cookie> <page>
<bean:message> <bean:resource> <bean:struts> <bean:include>
<bean:define> <bean:size> <bean:write>
1:<bean:header id="lang" name="Accpet-Language"/>
<bean:header>标签用于检索HTTP请求中的Header信息;
id:String类型的变量,存于page范围;
name:指定需要检索的Header信息;
2:<bean:parameter id="arg1" name="str1" value="guyong"/>
<bean:parameter>标签用于检索HTTP请求参数;
id:String类型的变量,存于page范围;
name:请求参数名称;
value:请求参数的默认值;
eg:
http://localhost:8080/myweb/index.jsp?str1=123&str1=456&str1=789
变量arg1的值为第一个str1,即arg1="123";
如果没有参数,arg1的值为默认值value,即arg1="guyong";
如果加上属性multiple="yes(可以是任一字符串)",id就不在是String型变量, 而是String[]型数组,arg1的值为{"123","456","789"};
3:<bean:cookie id="cookie" name="democookie" value="first">
<bean:cookie>标签可以检索保存在浏览器中的cookie;
id:Cookie类型的变量,存于page范围;
name:指定Cookie的名字;
value:指定Cookie的默认值。如果由name指定的Cookie不存在,就使用value指定的默认值;
eg:
if(cookie.getvalue().equals("first")){
Cookie c=new Cookie("democookie","second");
c.setComment("A test cookie");
c.setMaxAge(3600);
response.add(c);//此方法把Cookie写到客户浏览器端
}
<bean:cookie>标签先检索名为“democookie”的Cookie是否存在, 如果存在就把它的值赋给value,如果不存在就采用value的默认值“first”。
当第一次访问jsp页面时,由于客户端浏览器还不存在名为“democookie”的Cookie, 所以采用默认值“first”,同时jsp页面把名为“democookie”的cookie 写入客户端浏览器,当第二次访问这个jsp页面时,value为该cookie的值“second”;
4:<bean:page id="this_session" property="session"/>
<bean:page>标签用于检索jsp隐含对象;
id:定义一个引用隐含对象的变量,存于page范围;
property:指定隐含对象的名字,可选值包括:application,config,request,response,request和session;
eg:
Session Created=<bean:write name="this_session" property="creationTime"/>
以上代码引用jsp页面的session隐含对象,输出this_session变量的creationTime属性。
5:<bean:message>
<bean:message>标签用于输出Resource Bundle(资源文件)中的一条消息;先看资源文件:
<message-resources parameter="ApplicationResources"/>
<message-resources parameter="SpecialResources" key="special"/>
第一个资源文件没有没有指定key属性,因此是默认的资源文件, 文件名为ApplicationResources.properties,文件中定义了一条消息:hello=Hello,{0}
第二个资源文件指定了key属性“special”,名为SpecialResources.properties, 文件中定义了一条消息hello=Hello,everyone!
<bean:message>标签中指定消息key有三种方式:
<bean:message>直接指定消息key,eg:
<bean:message bundle="special(和资源文件中的key名称匹配)" key="hello"/>
对于带参数的复合消息,可以用<bean:message>标签的 arg0,arg1,arg2,arg3和arg4属性来设置参数,arg0代表第一个参数,一次类推。eg:
<bean:message key="hello" arg0="guyong"/>
以上代码没有bundle属性,因此从默认的资源文件中获取消息,于key匹配的为:hello=Hello,{0}
<bean:message>标签的arg0属性替代第一个参数{0},输出内容即:hello,guyong.
<bean:message>标签的name属性指定一个可以转化为字符串的变量,这个变量的值为消息key,eg:
<%request.setAttribute("stringBean","hello");%>
<bean:message bundle="special" name="stringBean"(相当于key=“hello”)/>
以上代码定义一个字符串变量stringBean值为“hello”,因此消息key为“hello”;
同时指定<bean:message>标签的name属性和property属性。 name属性指定一个javaBean,property指定javaBean的属性,该属性的值就是消息key。eg:
<%SomeBean bean=new SomeBean();
bean.setName("hello");
request.setAttribute("aaa",bean);%>
<bean:message bundle="special" name="aaa" property="name"/>
6:<bean:resource id="resource" name="two.jsp"/>
<bean:resource>标签用于检索Web资源的内容:
id:定义一个代表Web资源的路径。
name:指定Wwb资源的路径。
input:如果没有input属性,id为字符型变量,如果有为InputStream类型;
以上代码的定义的resource值为two.jsp的源文件内容;
7:<bean:struts>
<bean:struts>标签用于检索Struts框架内在的对象, 如ActionFormBean,ActionForward或ActionMapping.
eg:
<bean:struts id="forward" forward="index"/>
<bean:write name="forward" property="path"/>
id:定义一个page范围的变量,用来引用Strut框架的内在对象。
必须设置forward,formbean或mapping属性中的一个属性,来指定被引用的Struts内在对象。
formbean属性:指定ActionFormBean对象,和Struts配置文件的<form-bean>元素匹配。
forward属性:指定ActionForward对象,和Struts配置文件的 <global-forward>元素的<forward>子元素匹配。
mapping属性:指定ActionMapping对象,和Struts配置文件的 <action>元素匹配。
以上代码的输出结果为"/indextaglib.jsp";
8:<bean:include>
<bean:include id="str1" page="/two.jsp(URI)"/>
<bean:include id="str2" forward="全局转发的name"/>
<bean:include id="str3" href="完整路径"/>
id:定义一个变量,用来保存引用页面的资源;
和<jsp:include>很相似,区别在于<bean:include>是 把页面资源放在id变量中而不是直接显示到页面上;
9:<bean:define>
<bean:define>标签用于定义一个变量。
id:属性指定变量的名字,toScope属性指定这个变量的存放范围,如果没有toScope,则这个变量存放在page范围。给id属性定义的变量赋值有三种:
设置value属性,value代表这个变量的字符串值,eg:
<bean:define id="str1" value="guyong"/>
此时str1=“guyong”;
同时设置name和property属性。name属性指定一个javaBean, property属性指定javaBean中的一个属性,此时这个变量的值就等于javaBean中的属性值;
eg:
<bean:defile id="str1" name="javaBean名字" property="javaBean中的属性名"/>
同时设置name属性和type属性。name属性指定一个javaBean, type属性指定这个javaBean的完整的类名。
name属性指定的javaBean,在默认的情况下,<bean:define>标签会依次在page,request,session和application范围内寻找,也可以在<bean:define> 标签中设置scope属性,这样<bean:define>标签只会在指定的范围内寻找javaBean。
10:<bean:size id="length" name="map"/>
<bean:size>标签由于获取Map,Collection或数组的长度。 id:定义一个Integer类型的变量;
name:指定一个已存在的Map,Collection或数组变量;
eg:
<%HashMap map=new HashMap();
map.put("1","Line1");
map.put("2","Line2");
map.put("3","Line3");
request.setAttribute("map",map)%>
因此id=3;
11:<bean:write>
<bean:write>标签用于输出某个Bean或它的属性的内容;
<bean:size id="length" name="map"/>
<bean:write name="length"/>
<bean:wtite name="javaBean的名字" property="javaBean中属性的名字"/>
<%request.setAttribute("floatval",Float.valueOf("3.14159"));
Calendar gc=GregorianCalendar.getInstance();
gc.setTime(new Date(session.getCreationTime()));
request.setAttribute("now",gc)%>
<bean:write format="#.####" name="floatval"/>
<bean:write format="MM-dd-yyyy hh:mm:ss" name="now" property="time"/>
以上代码定义两个request范围变量floatval和now,输出结果为:"3.1416"和"05-25-2004 09:59:38"
<bean:write>还有一个filter属性,默认值是true。 如果filter属性为true,将把输出内容中的特殊HTML字符作为普通字符串来显示; 如果filter属性为false,则不会把输出内容中的特殊HTML符号转化为普通字符串。
评论0
最新资源