Bean标签库共有11个标签。这些标签可以完成如下五种工作:
1. 获得HTTP请求信息
2. 访问Java对象
3. 访问JSP内嵌对象和Struts配置对象
4. 访问Web资源和属性文件
5. 输出信息
下面我们就来分别介绍一下如何使用Bean标签库中的标签来完成上述的工作。
一、获得HTTP请求信息
使用Bean标签库中的标签可以访问Cookie、HTTP请求头以及请求参数。
1.
(1)id:用于保存Cookie对象的变量名。
(2)name:Cookie名
(3)value:Cookie的默认值。如果name所指的Cookie不存在,
2.
(1)id:用于保存HTTP请求头字段值的变量名。
(2)name:HTTP请求头字段名。
(3)value:HTTP请求头字段的默认值。如果name所指的HTTP请求头字段不存在,
3.
(1)id:用于保存HTTP请求参数值的变量名。
(2)name:HTTP请求参数名。
(3)value:HTTP请求参数值的默认值。如果name所指的HTTP请求参数不存在,
下面的例子演示了如何使用本节所讲的三个Bean标签来获得HTTP请求信息,在
在IE中输入如下的URL来测试httpRequestInfo.jsp: 要注意的是,上述的三个Bean标签都将变量保存到了page范围内(也就是JSP内嵌对象pageContext中),并且不能改变变量的保存范围。读者在使用这三个Bean标签时应注意这一点。 二、访问Java对象 1. (1)id:变量名。 (2)name:Java对象名。 (3)property:Java对象属性名。 (4)scope:name所指的Java对象所在的访问,如果不指定,默认是page范围。 (5)toScope:id所指的变量要保存的范围,如果不指定,默认是page范围。 2. (1)id:一个Integer变量 (2)name:集合或数据的变量名。 下面的例子演示了如何使用本节所讲的两个Bean标签来访问Java对象。在
Normal
0
7.8 磅
0
2
false
false
false
MicrosoftInternetExplorer4
在IE中输入如下的URL来测试accessJavaObject.jsp: (1)id:变量名。 (2)property:JSP内嵌对象名,必须是application、config,、request、response或session其中之一。 2. (1)id:变量名。 (2)formBean:struts-config.xml文件中的 (3)mapping:struts-config.xml文件中的 (4)forward:struts-config.xml文件中的 在使用 (1)同时使用了formBean、mapping和forward中的两个或三个。 (2)未指定formBean、mapping和forward (3)formBean、mapping或forward所指的标签不存在。 下面的例子演示了
http://localhost:8080/samples/httpRequestInfo.jsp?country=China
Normal
0
7.8 磅
0
2
false
false
false
MicrosoftInternetExplorer4
Normal
0
7.8 磅
0
2
false
false
false
MicrosoftInternetExplorer4
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><%@ page pageEncoding="GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>访问Java对象title>
head>
<body>
<%-- 建立actionform.HtmlTagsForm对象实例 --%>
<jsp:useBean id="htmlTagsForm" class="actionform.HtmlTagsForm"/>
<jsp:setProperty name="htmlTagsForm" property="name" value = "李宁"/>
<%-- 测试bean:define标签 --%>
<bean:define id="myBeanVar" name="htmlTagsForm" property="name"/>
${myBeanVar}
<%
String[] arr = new String[10];
pageContext.setAttribute("arr", arr);
%>
<%-- 测试bean:size标签 --%>
<bean:size id="length" name="arr"/>
${length}
body>
html>
http://localhost:8080/samples/accessJavaObject.jsp
1.
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> <%@ page pageEncoding="GBK"%>
<%@ 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>
Normal
0
7.8 磅
0
2
false
false
false
MicrosoftInternetExplorer4
1.
(1)id:变量名。
(2)href:Web资源的绝对路径。
(3)page:Web资源的相对路径。以“/”开头。
(4)forward:struts-config.xml文件
2.
(1)id:变量名。
(2)name:Web资源的相对路径。以“/”开头。
(3)input:如果指定input属性,id变量为java.io.InputStream类型,如果未指定input属性,id变量为String类型。
3.
(1)key:属性文件中的字符串信息键名。
(2)bundle:struts-config.xml文件中的
(3)name:用于获得键名的字符串变量名或对象实例变量名。
(4)property:获得key的属性名。如果name属性为对象实例变量名,则
(5)scope:
(6)arg0 ~ arg4:用于向带参数的字符串信息中传入参数值。分别对应于属性文件中的{0} ~ {4}。
下面的例子演示了本节所涉及到的三个标签的使用方法。在运行这个例子之前,先在
然后在struts-config.xml中的
Normal
0
7.8 磅
0
2
false
false
false
MicrosoftInternetExplorer4
最后在
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> <%@ page pageEncoding="GBK"%>
<%@ page import="actionform.HtmlTagsForm"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>访问Web资
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12921506/viewspace-541385/ ,如需转载,请注明出处,否则将追究法律责任。