三、访问JSP内嵌对象和Struts配置对象
1. <bean:page>标签
<bean:page>标签用来建立一个page范围的变量,并可通过这个变量来访问JSP的内嵌对象。这个标签有两个属性:
(1)id:变量名。
(2)property:JSP内嵌对象名,必须是application、config,、request、response或session其中之一。
2. <bean:struts>标签
<bean:struts>标签用来建立一个page范围的变量,并可通过这个变量来访问Struts的三个配置对象。这个标签有四个属性:
(1)id:变量名。
(2)formBean:struts-config.xml文件中的<form-bean>标签的name属性值。如果指定这个属性,<bean:struts>会创建org.apache.struts.action.ActionFormBean类型的对象实例。
(3)mapping:struts-config.xml文件中的<action>标签的path属性值。如果指定这个属性,<bean:struts>会创建org.apache.struts.action.ActionMapping类型的对象实例。
(4)forward:struts-config.xml文件中的<global-forwards>标签的子标签<forward>的name属性值。如果指定这个属性,<bean:struts>会创建org.apache.struts.action.ActionForward类型的对象实例。
在使用<bean:struts>标签时应注意,在满足下面三种情况中的一种,<bean:struts>就会抛出异常:
(1)同时使用了formBean、mapping和forward中的两个或三个。
(2)未指定formBean、mapping和forward
(3)formBean、mapping或forward所指的标签不存在。
下面的例子演示了<bean:page>和<bean:struts>标签的使用方法,在<samples工程目录>目录中建立一个accessEmbeddedObject.jsp文件,代码如下:
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>访问JSP内嵌对象和Struts配置对象</title>
</head>
<body>
<bean:page id="myRequest" property="request" />
myRequest.characterEncoding = ${myRequest.characterEncoding}
<br>
myRequest.contextPath = ${myRequest.contextPath}
<%
out.println(myRequest.getParameter("abc"));
%>
<bean:struts id = "myHtmlTagsForm" formBean="htmlTagsForm"/><br>
myHtmlTagsForm.type = ${myHtmlTagsForm.type}<br>
myHtmlTagsForm.getClass() = ${myHtmlTagsForm.class}
<bean:struts id = "myHtmlTags" mapping="/htmlTags"/><br>
myHtmlTags.type = ${myHtmlTags.type}<br>
myHtmlTags.getClass() = ${myHtmlTags.class}
<bean:struts id = "myNewProduct" forward="newProduct"/><br>
myNewProduct.path = ${myNewProduct.path}<br>
myNewProduct.getClass() = ${myNewProduct.class}
</body>
</html>
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->
四、访问Web资源和属性文件
1. <bean:include>标签
<bean:include>标签用于获得相对或绝对路径的Web资源,并将这些资源的内容保存到page范围的变量中。<bean:include>标签有四个常用的属性:
(1)id:变量名。
(2)href:Web资源的绝对路径。
(3)page:Web资源的相对路径。以“/”开头。
(4)forward:struts-config.xml文件<global-forwards>元素的子元素<forward>的name属性值。如果指定这个属性,<bean:include>标签会自动获得<forward>的path属性所指的Web资源的内容。
2. <bean:resource>标签
<bean:resource>标签和<bean:include>标签类似,也用来获得Web资源的内容,但和<bean:include>的不同之处是<bean:resource>在访问Web资源时(如JSP页面),并不执行这个JSP页面,而是将整个JSP页面的原始内容保存到变量中,而<bean:include>在访问这个JSP页面时,会先执行这个JSP页面,然后将JSP页面执行后的结果保存在变量中。因此,使用<bean:include>访问Web资源和在IE中输入相应的URL的效果是一样的。而<bean:resource>获得的是JSP页面的源代码。
<bean:resource>标签有三个属性:
(1)id:变量名。
(2)name:Web资源的相对路径。以“/”开头。
(3)input:如果指定input属性,id变量为java.io.InputStream类型,如果未指定input属性,id变量为String类型。
3. <bean:message>标签
<bean:message>标签用于从Java属性文件中获得字符串信息。要注意的是,<bean:message>标签获得字符串信息后,并不将所获得的信息保存在变量中,而是将其直接输出,也就是在执行JSP页面时,在生成客户端内容时,<bean:message>标签会被属性文件中的字符串信息代替。<bean:message>标签的常用属性如下:
(1)key:属性文件中的字符串信息键名。
(2)bundle:struts-config.xml文件中的<message-resources>标签的key值属值。如果不指定bundle属性,就使用默认的属性文件。
(3)name:用于获得键名的字符串变量名或对象实例变量名。<bean:message>标签除了从key属性中获得键名,还可以通过将key保存在指定范围的变量中,然后通过name属性获得这个key。
(4)property:获得key的属性名。如果name属性为对象实例变量名,则<bean:message>标签会从property所指的属性中获得key。
(5)scope:<bean:message>标签获得name变量的范围。默认值是page。
(6)arg0 ~ arg4:用于向带参数的字符串信息中传入参数值。分别对应于属性文件中的{0} ~ {4}。
下面的例子演示了本节所涉及到的三个标签的使用方法。在运行这个例子之前,先在<samples工程目录>"src"struts目录中建立一个MyResources.properties文件,并输入如下的内容:
myGreet = hello {0}
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->
然后在struts-config.xml中的<struts-config>元素中添加如下的子标签:
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->
最后在<samples工程目录>中建立一个accessResources.jsp文件,代码如下:
<%@ page import="actionform.HtmlTagsForm"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>访问Web资源和属性文件</title>
</head>
<body>
<bean:include id="myWebVar1"
href="http://localhost:8080/samples/simpleValidation.jsp" />
<bean:include id="myWebVar2" page="/htmlTags.jsp" />
<bean:include id="myWebVar3" forward="newProduct" />
${myWebVar1} ${myWebVar2} ${myWebVar3}
<bean:resource id="myResVar" name="/htmlTags.jsp" />
${myResVar}
<%-- 从MyResources.properties中获得信息 --%>
<bean:message bundle="my" key="greet" />
<%-- 从ErrorDescription.properties中获得信息 --%>
<bean:message key="error.email.invalid" />
<bean:message bundle="my" key="myGreet" arg0="李宁" />
<%
request.setAttribute("newGreet", "greet");
%>
<bean:message bundle="my" name="newGreet" />
<%
HtmlTagsForm form = new HtmlTagsForm();
form.setName("myGreet");
request.setAttribute("form", form);
%>
<%-- 从form对象的name属性获得key --%>
<bean:message bundle="my" name="form" property="name" arg0="李宁" />
</body>
</html>
在IE中输入如下的URL来测试accessResources.jsp:
http://localhost:8080/samples/accessResources.jsp
五、使用<bean:write>标签输出信息
<bean:write>用于输出字符串变量及对象变量的属性值。<bean:write>有如下六个常用的属性:
1. name:变量名(包括字符串变量或对象变量)。
2. property:如果name是对象变量,property表示name对象的属性。
3. filter:是否过滤输出内容中的HTML元素。如果filter为true,输出内容中的所有的HTML元素的敏感符号都会被替换成相应的字符串(如“<”被替换成了“<”,“>”被替换成了“>”)。
4. format:用于格式化输出内容的格式化字符串。
5. formatKey:保存在属性文件中的格式化字符串的key。
6. scope:name变量保存的范围。默认是page范围。
下面的例子演示了<bean:write>的常用方法。在运行这个例子之前,在<samples工程目录>"src"struts"MyResources.properties文件中加入如下的内容:
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->
在<samples工程目录>目录中建立一个beanWrite.jsp文件,代码如下:
<%@page import="actionform.HtmlTagsForm,java.util.*"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>测试bean:write</title>
</head>
<body>
<%
request.setAttribute("abcd", "myValue");
HtmlTagsForm form = new HtmlTagsForm();
form.setName("hello");
form.setWork("<b>工程师</b>");
request.setAttribute("form", form);
Calendar calendar = Calendar.getInstance();
request.setAttribute("calendar", calendar);
%>
<bean:write name="abcd" /><br>
<bean:write name="form" property="name" /><br>
<bean:write name="form" property="work" /><br>
<bean:write name="form" property="work" filter="false" /><br>
<bean:write name="calendar" property="time" format="yyyy-MM-dd HH:mm:ss" /><br>
<bean:write name="calendar" property="time"formatKey="formatDate" bundle="my" />
</body>
</html>
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->
在IE中输入如下的URL来测试beanWrite.jsp:
http://localhost:8080/samples/beanWrite.jsp
如果读者想了解关于bean标签库的更详细的信息,也可以访问官方的网站,URL如下:
http://struts.apache.org/1.2.9/userGuide/struts-bean.html