---
title: "struts1的常用的标签"
author: "xiajava2011"
date: "2011-03-26T22:42:21Z"
tags: []
categories: ["企业架构"]
slug: "struts1"
draft: false
lastmod: "2011-03-26T22:42:21Z"
id: 976331
url: "https://xiajava2011.iteye.com/blog/976331"
---

Bean标签库共有 11个标签。这些标签可以完成如下五种工作：

1.       获得 HTTP请求信息

2.       访问 Java对象

3.       访问 JSP内嵌对象和 Struts配置对象

4.       访问 Web资源和属性文件

5.       输出信息

下面我们就来分别介绍一下如何使用 Bean标签库中的标签来完成上述的工作。

一、获得 HTTP 请求信息


    使用 Bean标签库中的标签可以访问 Cookie、 HTTP请求头以及请求参数。

    1.  <bean:cookie> 标签

<bean:cookie>标签用于获得一个 Cookie对象，并创建一个 page范围的变量来保存这个 Cookie对象。 <bean:cookie>标签有三个常用属性：

（ 1） id：用于保存 Cookie对象的变量名。

（ 2） name： Cookie名

（ 3） value： Cookie的默认值。如果 name所指的 Cookie不存在， <bean:cookie>标签就会创建一个新的 Cookie对象，而 value属性的值就是这个 Cookie对象的 value属性值。如果忽略 value属性，当 <bean:cookie>标签未找到 name指写的 Cookie时，就会抛出一个 javax.servlet.jsp.JspException异常。因此，笔者建议在使用这个标签时加上 value属性。

2.  <bean:header> 标签

    <bean:header> 标签用于获得 HTTP请求头字段的值，并创建一个 page范围的变量来保存请求头字段的值。 <bean:header>标签有三个常用属性：

（ 1） id：用于保存 HTTP请求头字段值的变量名。

（ 2） name： HTTP请求头字段名。

（ 3） value： HTTP请求头字段的默认值。如果 name所指的 HTTP请求头字段不存在， <bean:header>标签就会将 value属性的值存在 page范围的变量中。如果不指定 value属性，且指定的 HTTP请求头字段不存在时， <bean:header>标签就会抛出 javax.servlet.jsp.JspException异常。

3. <bean:parameter> 标签

<bean:parameter>标签用于获得 HTTP请求参数的值，并创建一个 page范围的变量来保存所获得的 HTTP请求参数的值。 <bean:parameter>标签有三个常用属性：

（ 1） id：用于保存 HTTP请求参数值的变量名。

（ 2） name： HTTP请求参数名。

（ 3） value： HTTP请求参数值的默认值。如果 name所指的 HTTP请求参数不存在， <bean:parameter >标签就会将 value属性的值存在 page范围的变量中。如果不指定 value属性，且指定的 HTTP请求参数不存在时， <bean:parameter>标签就会抛出 javax.servlet.jsp.JspException异常。

下面的例子演示了如何使用本节所讲的三个 Bean标签来获得 HTTP请求信息，在 <samples工程目录 >中建立一个 httpRequestInfo.jsp文件，代码如下：
<!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->   <% @ page pageEncoding = " GBK " %>
   <% @ taglib uri = " http://struts.apache.org/tags-bean "  prefix = " bean " %>
   < html >
       < head >
           < title > 获得HTTP请求信息 </ title >
       </ head >
       < body >
           <% --   测试bean:cookie标签  -- %>
           < bean:cookie  id ="myCookie"  name ="name"  value ="default"   />   
           <%  
               if  (myCookie.getValue().equals( " default " )) 
              { 
                  Cookie cookie  =   new  Cookie( " name " ,  " newCookie " ); 
                  cookie.setMaxAge( 1000 ); 
                  response.addCookie(cookie); 
              } 
           %>
      ${myCookie.value}  <% --  用EL输出myCookie的value属性值  -- %>
       <%  
           //  ${myCookie.value}相当于如下Java代码
          Cookie cookie  =  (Cookie)pageContext.getAttribute( " myCookie " );
           out.println(cookie.getValue());         
       %>   < br >
        <% --   测试bean:header标签  -- %>
       < bean:header  id ="userAgent"  name ="user-agent"  value ="unknown" />
      ${userAgent} < br >    <% --  用EL输出userAgent的值  -- %>
       <% --  测试bean:parameter标签   -- %>
       < bean:parameter  id ="myCountry"  name ="country"  value ="unknown" />
      ${myCountry}     <% --  用EL输出myCountry的值  -- %>
       </ body >
   </ html >

在IE中输入如下的URL来测试httpRequestInfo.jsp：

http://localhost:8080/samples/httpRequestInfo.jsp?country=China
<!-- ><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><!--><!-- ><!--> <!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; 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 */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> <!-- > <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <!-->
<!-- ><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><!--><!-- ><!--> <!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; 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 */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> <!-- > <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <!-->

    要注意的是，上述的三个 Bean标签都将变量保存到了 page范围内（也就是 JSP内嵌对象 pageContext中），并且不能改变变量的保存范围。读者在使用这三个 Bean标签时应注意这一点。

二、访问 Java 对象

1. <bean:define>标签

<bean:define>标签用来将 Java对象的属性值保存在变量中。 <bean:define>标签有五个常用属性：

（ 1） id：变量名。

（ 2） name： Java对象名。

（ 3） property： Java对象属性名。

（ 4） scope： name所指的 Java对象所在的访问，如果不指定，默认是 page范围。

（ 5） toScope： id所指的变量要保存的范围，如果不指定，默认是 page范围。

 2. <bean:size>标签

<bean:size>标签用来获得集合（ Map、 Collection）或数组的长度。 <bean:size>标签有两个常用的属性：

（ 1） id：一个 Integer变量

（ 2） name：集合或数据的变量名。

下面的例子演示了如何使用本节所讲的两个 Bean标签来访问 Java对象。在 <samples工程目录 >目录中建立一个 accessJavaObject.jsp文件 ， 代码如下 ：

 
<!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><% @ 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 ;
          pageContext.setAttribute( " arr " , arr);
        %>     
       <% --   测试bean:size标签  -- %>     
        < bean:size  id ="length"  name ="arr" />
       ${length}
       </ body >
   </ html >

<!-- ><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><!--><!-- ><!--> <!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; 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 */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> <!-- > <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <!-->

在 IE中输入如下的 URL来测试 accessJavaObject.jsp：

http://localhost:8080/samples/accessJavaObject.jsp

 
<!-- ><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><!--><!-- ><!--> <!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; 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 */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> <!-- > <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <!-->

三、访问 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文件，代码如下：

<!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->   <% @ 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 >

<!-- ><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><!--><!-- ><!--> <!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; 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 */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> <!-- > <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <!-->

四、访问 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文件，并输入如下的内容：   

<!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->greet  =  hello world
myGreet  =  hello { 0 }

<!-- ><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><!--><!-- ><!--> <!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; 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 */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> <!-- > <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <!-->
然后在struts-config.xml中的<struts-config>元素中添加如下的子标签：

<!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->   < message-resources  parameter ="struts.MyResources"  key ="my"   />

<!-- ><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><!--><!-- ><!--> <!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; 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 */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> <!-- > <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <!-->

最后在 <samples工程目录 >中建立一个 accessResources.jsp文件，代码如下：
<!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->   <% @ page pageEncoding = " GBK " %>
   <% @ 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"   />