bean:cookie

cookie最早是由Netscape公司提出来的,用来存储客户的少量状态信息。如果您对cookie的具体细节感兴趣可以查阅cookie spec。

bean: cookie标签取回请求中名称为name的cookie的值。如果没有指定multiple属性则依据刚取回的值创建一个Cookie类型的bean。 如果指定了multiple属性则依据刚取回的值创建一个Cookie[]类型的数组。然后用id属性值将Cookie或Cookie[]绑定到page 作用域中(这种绑定是为了其它标签能够使用该值),并创建对应的scripting变量(这种变量是为了JSP脚本能够使用该值)。

下面的代码片段示例了如何使用bean:cookie标签读取名为JSESSIONID的cookie的值,并且使用了两种方式进行了输出:

<logic:present cookie="JSESSIONID">
  <bean:cookie name="JSESSIONID" id="jSession">

  这个cookie的名称是<bean:write property="name" name="jSession">,值为<bean:write property="value" name="jSession">。


  <%
    String name = jSession.getName();
    String value = jSession.getValue();
    out.println("这个cookie的名称是"+name+",值为"+value+"。
");	
  %>
</bean:write>
</bean:write></bean:cookie></logic:present>

bean:define

bean: define标签在toScope(如果没有指定值就使用page作用域)指定的作用域中创建一个新属性,同时创建一个scripting变量。我们可以 通过id值使用它们。新创建的属性可以由其它标签使用,而新创建的scripting变量可以由JSP脚本使用。

我们可以使用三种方式为新创建的属性和scripting变量赋值:

  • 通过该标签的name、property和scope取回值,并且保持类型的一致性,除非取回的值为Java的原始类型,这时会使用适合的包装器类对这些值进行包装。
  • 通过该标签的value指定值,这时新创建的属性和scripting变量的类型为java.lang.String。
  • 通过在该标签的体中嵌入值,这时新创建的属性和scripting变量的类型为java.lang.String。

下面的代码片段示例了如何使用bean:define标签创建新属性values和新scripting变量values,它将listForm中persons的值取出来赋给values:

<bean:define type="java.util.List" property="persons" name="listForm" id="values"> 

</bean:define>

下面给出ListForm的代码片段以帮助您更好的理解,其中Person是一个只有id和name两个属性的简单bean:

public class ListForm extends ActionForm {
  private List<person> persons = null;
  public List<person> getPersons() {
    return persons;
  }
  public void setPersons(List<person> persons) {
    this.persons = persons;
  }
  public void reset(ActionMapping mapping, HttpServletRequest request) {
    persons = null;
  }
} 
</person></person></person>

下面的代码片段示例了logic:iterate标签如何使用bean:define标签创建的新属性values:

<logic:iterate name="values" id="person">
  <bean:write property="id" name="person">

</bean:write>
</logic:iterate>

下面的代码片段示例了JSP脚本如何使用bean:define标签创建的新scripting变量values:

<%
  Person p = new Person();
  for(int i=0;i

bean:header

bean: header标签取回请求中名称为name的header的值。如果没有指定multiple属性则依据刚取回的值创建一个String类型的bean。 如果指定了multiple属性则依据刚取回的值创建一个String[]类型的数组。然后用id属性值将String或String[]绑定到page 作用域中(这种绑定是为了其它标签能够使用该值),并创建对应的scripting变量(这种变量是为了JSP脚本能够使用该值)。

下面是我的浏览器发送的header的内容,这些内容和浏览器有关,因此您的浏览器发送的内容可能和下面列出的不同。不过这没有关系,因为要理解bean:header标签您只要对这些内容有一个大概的认识就足够了。

accept: */* 
accept-language: zh-cn 
accept-encoding: gzip, deflate 
user-agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322) 
host: localhost:8080 
connection: Keep-Alive

下面的代码片段示例了如何使用bean:header标签读取名为User-Agent的header的值,并且使用了两种方式进行了输出:

<logic:present header="User-Agent">

  您的浏览器是<bean:header name="User-Agent" id="userAgent">
  <bean:write name="userAgent">。


  <%
    out.println("您的浏览器是"+userAgent+"。
");	
  %>
</bean:write>
</bean:header></logic:present>

bean:include

bean:include标签对指定url(由forward、href或page确定)处的资源做一个请求,将响应数据作为一个String类型的bean绑定到page作用域,同时创建一个scripting变量。我们可以通过id值访问它们。

下面的代码片段示例了bean:include标签的用法,其中include.txt文件包含要include的内容,然后将这些内容输出:

<bean:include page="/include.txt" id="value">

<bean:write name="value">


<%
  out.println(value);	
%>
</bean:write></bean:include>

bean:message

bean:message标签用来从指定的locale中取回国际化的消息并输出,在这个过程中我们还可以传递五个以内的参数。message key可以通过key直接指定,也可以通过name和property间接的指定。

bean:message标签有两种指定message key的方式,一是通过key属性直接指定;二是通过name和property属性间接的指定,其中message key是在message resources文件中定义的。

我们可以在struts-config.xml文件中使用<message-resources>来设置message resources文件。</message-resources>

为 了介绍该标签我使用了三个message resources文件,三个文件的名字分别为Resources.properties、Resources_en.properties和 Resources_zh.properties。在struts-config.xml文件中的设置(这里不用设置三个,struts会依据 locale自动找到对应的文件)如下:

<message-resources parameter="Resources">
</message-resources>

三个message resources文件中定义的message key为:

resource=Resources.properties.
from=Resources.properties.

from=Resources_en.properties.

from=Resources_zh.properties.

下面的代码片段示例了bean:message标签的用法:

<bean:message key="from">

<bean:message key="resource">

<html:link action="/locale?language=en">English</html:link>
<html:link action="/locale?language=zh">Chinese</html:link>
</bean:message></bean:message>

上面的代码中含有改变locale的两个html:link标签,要使它们工作您的struts-config.xml文件中必须含有下面定义的form和action:

<form-bean type="org.apache.struts.action.DynaActionForm" name="localeForm">
  <form-property type="java.lang.String" name="language">
  <form-property type="java.lang.String" name="country">

  <form-property initial="/message.jsp" type="java.lang.String" name="page">
</form-property>

<action scope="request" name="localeForm" type="org.apache.struts.actions.LocaleAction" path="/locale"></action>
</form-property></form-property></form-bean>

在不同的locale下我们得到了如下的两个结果:

在locale为zh时的结果:
Resources_zh.properties.
Resources.properties.
在locale为en时的结果:	
Resources_en.properties.
Resources.properties.

让我们来看一下在locale为zh时如何得到的是上面的结果。因为locale为zh所以 <bean:message key="from">
先找Resources_zh.properties这个文件从中得到form键的值。而< bean:message key="resource"/>
也会先找Resources_zh.properties这个文件但这次没有找到 resource键,这时Struts会到Resources.properties这个文件中找,很幸运这里找到了。如果还没有找到,或message resource文件不存在就会抛出异常。当locale为en时类似,您可以自己试试。 </bean:message>

bean:page

bean: page标签将页上下文中的application、config、request、response 或 session取出,然后用id属性值将它们绑定到page作用域中(这种绑定是为了其它标签能够使用该值),并创建对应的scripting变量(这种 变量是为了JSP脚本能够使用该值)。

下面的代码片段示例了bean:page标签取出response,然后使用bean:write标签将response的characterEncoding和contentType属性输出:

<bean:page property="response" id="res">

<bean:write property="characterEncoding" name="res">

<bean:write property="contentType" name="res">


<%
  String characterEncoding = res.getCharacterEncoding();
  String contentType = res.getContentType();
  out.println(characterEncoding+"
");	
  out.println(contentType+"
");	
%>
</bean:write></bean:write></bean:page>

您可以用和上面类似的代码访问application、config、request 或 session中的任何一个对象。

bean:parameter

bean: parameter标签取回请求中的参数值。如果没有指定multiple属性则依据刚取回的值创建一个String类型的bean。如果指定了 multiple属性则依据刚取回的值创建一个String[]类型的数组。然后用id属性值将String或String[]绑定到page作用域中 (这种绑定是为了其它标签能够使用该值),并创建对应的scripting变量(这种变量是为了JSP脚本能够使用该值)。

下 面的两个代码片段使用相同的url传递参数,url的形式为http://127.0.0.1:8080/struts- demo/parameter.jsp?param=1&param=2&param=3。前面的代码片段中没有指定multiple属 性,因此p是String类型而且仅仅读取了参数的第一个值。后面的代码片段中指定了multiple属性的值,因此ps是String[]类型的包含所 有参数的值。

<bean:parameter name="param" id="p">
<bean:write name="p">
</bean:write></bean:parameter>
<bean:parameter name="param" multiple="true" id="ps">
<logic:iterate name="ps" id="p">
  <bean:write name="p">

</bean:write>
</logic:iterate></bean:parameter>

bean:resource

bean: resource标签取回指定的web应用程序的资源,以InputStream或String的形式保存到page作用域中并且创建scripting 变量。采用什么形式取决于标签的input属性,如果指定input则以InputStream的形式保存,如果没有指定input则以String的形 式保存。

下面的两个代码片段示例了bean:resource标 签,其中resource.txt是要使用的资源文件。前面的代码片段中没有指定input属性,因此以String的形式处理资源文件,bean: write标签输出资源文件的内容。后面的代码片段中指定了input属性的值,因此以InputStream的形式使用资源文件,两个bean: write标签分别输出InputStream对象的实例名(如java.io.ByteArrayInputStream@16dadf9)和类名(如 class java.io.ByteArrayInputStream)。

<bean:resource name="/resource.txt" id="str">

<bean:write name="str">


<%
  out.println(str+"
");
%>
</bean:write></bean:resource>
<bean:resource name="/resource.txt" input="true" id="is">

<bean:write name="is">

<bean:write property="class" name="is">

<%
  out.println(is+"
");
  out.println(is.getClass()+"
");
%>
</bean:write></bean:write></bean:resource>

bean:size

bean:size标签创建一个java.lang.Integer类型的bean,这个bean的值为该标签指定的Collection或Map中所含元素的个数。 这可以和logic:iterate标签配合使用,因为logic:iterate标签不能得到所叠代的集合的元素的个数,这有时候很不方便。

下面的代码片段示例了bean:size标签取出persons中还有元素的个数,其中listForm和persons的定义参见bean:define标签部分:

<logic:notempty property="persons" name="listForm">		
  <bean:size property="persons" name="listForm" id="size">
  <bean:write name="size">		
</bean:write>  
</bean:size></logic:notempty>

bean:struts

bean:struts标签取回Struts的内部对象formBean、forward或mapping的值,然后用id绑定到page作用域中(这种绑定是为了其它标签能够使用该值),并创建对应的scripting变量(这种变量是为了JSP脚本能够使用该值)。

下面的代码片段示例了bean:struts标签取出listForm对象,让我们先来看一下listForm的定义在读代码:

<bean:struts formbean="listForm" id="listFormBean">
name:<bean:write property="name" name="listFormBean">

type:<bean:write property="type" name="listFormBean">

dynamic:<bean:write property="dynamic" name="listFormBean">

</bean:write></bean:write></bean:write></bean:struts>

上面代码运行的结果为:

name:listForm 
type:org.solo.struts.form.ListForm
dynamic:false

bean:write

bean:write标签将指定的bean的属性值写到当前的JspWriter中,并且可以对输出进行格式化。

下面的代码片段示例了bean:write标签输出User-Agent:

<logic:present header="User-Agent">
  <bean:header name="User-Agent" id="header">
  <bean:write name="header">
</bean:write>
</bean:header></logic:present>

下面的代码片段示例了bean:write标签格式化输出当前日期,其中now是在 DataForm中定义的一个java.util.Date类型的域(值为new Date()),format.date.standard是在资源文件中的一个键(format.date.standard=yyyy-MM- dd):

<bean:define property="now" name="dataForm" id="date">

<bean:write name="date">

<bean:write format="MM/dd/yyyy" name="date">

<bean:write formatkey="format.date.standard" name="date">
</bean:write></bean:write></bean:write></bean:define>

上面代码运行的结果为:

Sun Jun 04 17:04:05 CST 2006
06/04/2006
2006-06-04

html:base

虽然这里的标题是html:base标签,但是这里也是这篇指南要介绍的第一个Struts html标签。因此我想在这里从整体上简单的介绍一下,最后给出一个对照表。其中的绝大多数标签就不一一介绍了,要介绍的也不会在这里介绍,而是将其独立出来以显重要性。

从 用户处收集数据是动态web应用非常重要的一个方面,因此构建输入表单也就自然而然的成为struts框架的一个重要内容。Struts html标签库含有创建Struts输入表单的标签,和其它标签库(bean、logic、nested和tiles)中的标签一起协作就可以产生基于 html的用户界面。

下面的对照表会使您对Struts html标签库有一个整体的印象:


图示 1. Struts HTML标签和HTML元素对照表


html:cancel

html: cancel标签生成一个取消按钮。当点击该按钮后action servlet会绕过相应的form bean的validate()方法,同时将控制权交给相应的action。在该action中可以使用Action.isCancelled (HttpServletRequest)方法判断是否被取消了。如果返回true表示这个action被取消了,否则表示这个action没有被取消。

请注意,如果您修改了html:cancel标签的property属性值,那么struts提供的cancel探测机制就失效了,您自己必须提供类似的机制。

下 面是可取消的action的配置文件,注意<set-property value="true" property="cancellable">这一行,如果不添加Struts会抛出 org.apache.struts.action.InvalidCancelException异常。这是我在完成本指南的过程中发现的唯一向下不兼 容的地方。</set-property>

<action path="/cancel">
  type="org.solo.struts.action.CancelAction" name="cancelForm" scope="request">
  <set-property value="true" property="cancellable">
  <forward path="/cancel.jsp" name="success">
</forward>
</set-property></action>

下面是html:cancel标签的代码:

<html:cancel>取消</html:cancel>

下面是对应的action中的代码:

if(isCancelled(request)){			
  //action被取消时要做的事情写在这里
  return mapping.findForward("cancel");
}else{
  //action没有被取消时要做的事情写在这里
  return mapping.findForward("success");
}

html:select

html: select标签生成一个select元素。是单选还是多选取决于该标签的multiple属性。如果指定了multiple="true"则为多选,此 时对应的属性应该是一个数组。如果没有指定multiple="true"则为单选,此时对应的属性应该是标量。

注意:为了正确的处理没有做任何的选择的情况,在ActionForm中的reset()方法中必须将标量属性设置为默认值而将数组的长度置为0。

另外的一个重要问题就是struts如何生成option元素了,这个任务struts交给了html:option、html:options和html:optionsCollection三个标签。

html:option标签

html:option标签生成一个HTML的option元素。该标签必须嵌在html:select标签中。它的显示文本来自其标签体,也可以来自于资源文件。它的value属性用来指定什么值将要被提交。

<html:option value="one">one</html:option>
<html:option value="two">two</html:option>

html:options标签

html:options标签生成多个HTML的option元素。该标签必须嵌在html:select标签中。而且工作方式有些特殊,它的name与property属性和其它标签的name与property属性意义不一致,让我们具体看一下它的工作方式。

  • 指定collection属性
  • 没有指定collection属性

指定collection属性

让我通过示例来介绍在指定collection属性时该标签的工作方式,首先要说明selectForm中的persons和listForm中的persons完全一致。请参见bean:define标签。

下 面的代码先利用bean:define标签将selectForm中的persons取到page作用域中,然后html:options标签再依据 collection="personCollection"选中这个persons并将其中的每一个对象(Person类型)生成一个option元 素。该标签的property="id"表示persons中的对象(Person类型)的id属性将作为option元素的value值。该标签的 labelProperty="name"表示persons中的对象(Person类型)的name属性将作为option元素的label值。

当 这个select提交时所选择的值将被提交到selectForm(name="selectForm")中的person对象(这是在 SelectForm中声明的一个Person类型的域专门用来接收提交的值)的id属性中(property="person.id")。

<bean:define property="persons" name="selectForm" id="personCollection">
<html:select size="1" property="person.id" name="selectForm">
  <html:options labelproperty="name" property="id" collection="personCollection"> 
</html:options>
</html:select></bean:define>

没有指定collection属性

让我通过示例来介绍没有指定collection属性时该标签的工作方式,先来看看ids和names的定义:

private List<string> ids = null;
  private List<string> names = null;
</string></string>

上面的代码来自SelectForm,其中ids是一个String的列表,names也是一个 String的列表。我们暂时假定这两个列表含有相同数目的元素。有了这些让我们开始介绍下面的代码。html:options标签用ids中的第i个值 作为option元素的value值同时使用names中相同位置的值(第i个值)作为option元素的label值。如果ids比names长那么多 出的ids中的值将即作为option的value又作为option的label。如果ids比names短那么多出的names的值会被丢掉。

当 这个select提交时所选择的值将被提交到selectForm(name="selectForm")中的person对象(这是在 SelectForm中声明的一个Person类型的域专门用来接收提交的值)的id属性中(property="person.id")。

<html:select size="1" property="person.id" name="selectForm">
  <html:options labelproperty="names" property="ids"> 
</html:options>
</html:select>

html:optionsCollection标签

html: optionsCollection标签生成多个HTML的option元素。该标签必须嵌在html:select标签中。它的功能和html: options标签的相同,但是它的name与property属性和其它标签的name与property属性意义一致,理解起来比较自然。

让 我通过示例来介绍html:optionsCollection标签的用法。首先依据name="selectForm"和property= "persons"取到selectForm中的persons列表,然后将列表中的对象(Person类型)的id属性作为option元素的 value值(value="id"),将列表中的对象(Person类型)的name属性作为option元素的label值(label= "name")。

<html:select size="1" property="person.id" name="selectForm">
  <html:optionscollection value="id" label="name" property="persons" name="selectForm"> 
</html:optionscollection>
</html:select>

下面是一个多选的示例,虽然示例中使用了html:options标签,但是html: option和html:optionsCollection也能够用来多选。而且您还必须意识到html:option、html:options和 html:optionsCollection这三个标签可以同时使用。代码中的personIds是SelectForm中声明的一个String[] 类型的数组用来接收提交的多个值。

<html:select size="2" multiple="true" property="personIds" name="selectForm">
  <html:options labelproperty="names" property="ids"> 
</html:options>
</html:select>

html:checkbox

html:check标签生成一个checkbox。这里的value值可以是true,yes或on。如果您要提交其它的值(如某种形式的标识)应该考虑使用html:multibox标签。

注意:为了正确的处理没有选中的checkbox您必须在reset()中设置对应的属性为false。

下面的代码示例了html:checkbox标签的用法,其中CheckboxForm中声明了三个boolean类型的域,如下:

private boolean one = false;
  private boolean two = false;
  private boolean three = false;
<html:checkbox property="one" name="checkboxForm">One</html:checkbox>
<html:checkbox property="two" name="checkboxForm">Two</html:checkbox>
<html:checkbox property="three" name="checkboxForm">Three</html:checkbox>

如果选中后被提交则相应的属性的值为true。

html:radio

html:radio标签生成一个radio。主要的用法有两种,下面我们通过代码来示例。

下面的代码示例了html:radio标签的一般用法,如果被提交则选中的radio的value值将被提交到radioForm中的id中。

<html:radio value="00001" property="id" name="radioForm">One</html:radio>
<html:radio value="00002" property="id" name="radioForm">Two</html:radio>

下面的代码示例了html:radio标签的典型用法,其中的persons和bean: define标签中的一致,您可以参考bean:define标签。我只介绍这个 <html:radio value="id" property="id" idname="person"> ,idName指定html:radio要使用的bean(这里为person),value="id"表示person的 id属性将作为radio元素的value值而property="id"表示提交时选中的radio的值将被提交给radioForm中的id属性。 </html:radio>

<logic:notempty property="persons" name="radioForm">		
  <logic:iterate property="persons" name="radioForm" id="person">
    <html:radio value="id" property="id" idname="person">
      <bean:write property="name" name="person">
    </bean:write> 
  </html:radio>		
</logic:iterate>
</logic:notempty>

html:multibox

html:multibox标签生成多个checkbox。当您要使用大量的checkbox时使用这个标签非常方便,可以使您避免在ActionForm中声明大量的boolean类型的变量,带之以一个数组就行了。

注意:为了正确的处理没有选中的checkbox您必须在reset()中设置数组的长度为0。

下面的代码示例了html:multibox标签的一般用法,如果被提交则选中的所有checkbox的value值将被提交到multiboxForm中的selectedItems中,这是一个String[]数组。

<html:multibox value="00001" property="selectedItems" name="multiboxForm">
<html:multibox value="00002" property="selectedItems" name="multiboxForm">
</html:multibox></html:multibox>

下面的代码示例了html:multibox标签的典型用法:

<logic:iterate property="persons" name="multiboxForm" id="person"> 
  <html:multibox property="selectedItems">
    <bean:write property="id" name="person">
  </bean:write> 
  <bean:write property="name" name="person"> 
</bean:write>
</html:multibox></logic:iterate>

html:link

html:link标签生成一个锚点()元素。我从html:link标签如何构造基本url和如何构造query参数两个方面来介绍这个标签。

构造基本url是依据该标签的如下四个属性进行的,这四个属性一次只能出现一个。让我们看看它们:


forward,指定一个全局ActionForward的名称。另外如果forward是module相关的则该forward必须指向一个action而不能是一个页面。 action,指定一个Action的名称。 href,struts会直接使用这个值而不会对其进行任何处理。 page,指定一个页面的路径,必须以/开始。

构造query参数,下面列举了可能的形式,其中baseurl只是一个占位符:


baseurl?p=00001(单参单值) baseurl?p1=00001&p2=00002&p3=00003(多参单值) baseurl?p=00001&p=00002&p=00003(单参多值) baseurl?p1=00001&p2=00002&p=00001&p=00002&p=00003(混合)

下面我们以代码示例来说明每一种html:link的用法,首先让我们花点时间看看相关action中的代码:

public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {

  DataForm dataForm = (DataForm) form;

  dataForm.setParam("00001");

  HashMap paramSingleMap = new HashMap();
  paramSingleMap.put("p1","00001");
  paramSingleMap.put("p2","00002");
  paramSingleMap.put("p3","00003");		
  dataForm.setParamSingleMap(paramSingleMap);

  HashMap paramMultiMap = new HashMap();
  paramMultiMap.put("p",new String[]{"00001","00002","00003"});		
  dataForm.setParamMultiMap(paramMultiMap);

  HashMap paramSMMap = new HashMap();
  paramSMMap.put("p1","00001");
  paramSMMap.put("p2","00002");
  paramSMMap.put("p",new String[]{"00001","00002","00003"});		
  dataForm.setParamSMMap(paramSMMap);

  List<person> persons = new ArrayList<person>();		
  Person person1 = new Person();
  person1.setId("00001");
  person1.setName("赵辰");		
  Person person2 = new Person();
  person2.setId("00002");
  person2.setName("李为芳");		
  Person person3 = new Person();
  person3.setId("00003");
  person3.setName("王微");		
  persons.add(person1);
  persons.add(person2);
  persons.add(person3);		
  dataForm.setPersons(persons);

  return mapping.findForward("success");
}
</person></person>
<html:link linkname="top">

<html:link anchor="top" page="/link.do">Go Top</html:link>
</html:link>
<html:link paramproperty="param" paramname="dataForm" paramid="p" page="/link.do">单参单值</html:link>

<html:link property="paramSingleMap" name="dataForm" page="/link.do">多参单值</html:link>

<html:link property="paramMultiMap" name="dataForm" page="/link.do">单参多值</html:link>

<html:link property="paramSMMap" name="dataForm" page="/link.do">混合</html:link>

下面的代码示例了html:link标签的indexed属性和indexId属性的用法,这两个属性只有html:link标签嵌套在logic:iterate标签中时才可用。

<logic:iterate property="persons" name="dataForm" id="person">
  <html:link indexid="number" indexed="true" paramproperty="id" paramname="person" paramid="person" action="/link.do">person</html:link>


</logic:iterate>

html:rewrite标签和html:link标签类似只是不生成锚点(),而是简单的输出字符串。

html:errors

html:errors标签和html:messages标签的功能相似,所以我们放到一起来介绍。

html:errors标签将由name属性指定的ActionMessages、ActionErrors、String和String[]直接输出到页面中。

html:messages标签将用由name属性(注意message属性值对它的影响)指定的ActionMessages、ActionErrors、String和String[]创建一个新的属性和scripting变量,使用id属性值作为名称。

html:errors标签和html:messages标签的property属性是用来为errors和messages分类的。我们可以给这两个标签指定property属性,以便只显示某一类的错误或消息。

在资源文件增加了如下的内容:

# -- standard errors --
errors.header=• errors.prefix=
• errors.suffix=
errors.footer=

error=error with none value . 

error1=error1 with one value is {0} . 

error2=error2 with two values are {0} , {1} . 

error3=error3 with three values are {0} , {1} , {2} . 

error4=error4 with four values are {0} , {1} , {2} ,{3} .


下面的代码示例了actionErrors的构造:

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
  ActionErrors actionErrors = new ActionErrors();
  actionErrors.add("property1",
  new ActionMessage("error"));
  actionErrors.add("property2",
  new ActionMessage("error1","value0"));
  actionErrors.add("property2",new ActionMessage("error2","value0","value1"));
  actionErrors.add("property3",new ActionMessage("error3","value0","value1","value2"));
  actionErrors.add("property3",new ActionMessage("error4","value0","value1","value2","value3"));

  actionErrors.add("property4", new ActionMessage("error1",new Object[]{"value0"}));
  actionErrors.add("property4", new ActionMessage("error2",new Object[]{"value0","value1"}));
  actionErrors.add("property4", new ActionMessage("error3",new Object[]{"value0","value1", "value2"}));
  actionErrors.add("property5",new ActionMessage("error4",new Object[]{"value0","value1", "value2","value3"}));

  actionErrors.add("notBundle",new ActionMessage("not a bundle key",false));
  return actionErrors;
}

errors标签代码示例:

<html:errors>


<html:errors property="property4">
</html:errors></html:errors>

messages标签代码示例:

<logic:messagespresent>
  </logic:messagespresent>
    •  <html:messages id="message">
    • <bean:write name="message"></bean:write>
     </html:messages>
    
    
    
    <logic:messagespresent> 
    
     </logic:messagespresent> 
    •  <html:messages property="property4" id="message">
    • <bean:write name="message"></bean:write>
     </html:messages>


    图示 2. 上面的两段代码示例运行结果相同,如下所示:


    html:javascript

    html:javascript标签生成用于校验的javascript脚本代码。

    下 面的代码片段示例了html:javascript标签的基本用法,其中formName属性的值是在validation.xml文件的< formset>中定义的form的名称。有一点值得注意的是在确定 <formset> 时要使用合适的language属性值。 </formset>

    <html:javascript formname="dataForm">
    </html:javascript>

    html:xhtml

    html: xhtml标签在页面中一出现就是告诉该页的所有其它的Struts html标签以XHTML1.0兼容的形式输出。这和将html:html标签的xhtml属性值指定为true有些类似。如果上述情况都没有出现, Struts html标签将以html4.01兼容的形式输出。

    logic:iterate

    logic:iterate标签用来迭代集合,您可以使用如下方式来为这个标签指定其要叠代的集合:

    • 使用一个运行时表达式,这个表达式的值是一个集合。
    • 用name属性引用一个JSP Bean,这个JSP Bean本身就是一个集合。
    • 用name属性引用一个JSP Bean,这个JSP Bean的一个属性是一个集合,这时可以联合使用property来指定这个集合。

    上面所提到的集合可以是:

    • 对象类型或原子类型的数组(Array)。
    • java.util.Collection的实现,包括ArrayList,Vector。
    • java.util.Enumeration的实现。
    • java.util.Iterator的实现。
    • java.util.Map的实现,包括HashMap,Hashtable和TreeMap。

    如果您叠代的集合中含有null的值,这时需要采取一定的措施,因为这时logic:iterate不会在page作用域中创建对象。一般是使用<logic:present>标签或<logic:notpresent>标签来判断一下。</logic:notpresent></logic:present>

    下面是logic:iterate叠代ArrayList的示例的对象引用关系和部分代码:


    图示 3. logic:iterate中对象的引用关系


    图中的persons列表是在ListAction中填充的,在这里只是简单的加入了三个Person对象,在实际的应用中这些数据应该取自数据库。具体的代码如下:

    public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,	HttpServletResponse response) {
    
      ListForm listForm = (ListForm) form;
    
      List<person> persons = new ArrayList<person>();
    
      Person person1 = new Person();
      person1.setId("00001");
      person1.setName("赵辰");
    
      Person person2 = new Person();
      person2.setId("00002");
      person2.setName("李为芳");
    
      Person person3 = new Person();
      person3.setId("00003");
      person3.setName("王微");
    
      persons.add(person1);
      persons.add(person2);
      persons.add(person3);
    
      listForm.setPersons(persons);
    
      return mapping.findForward("success");
    }
    </person></person>

    标签输出的结果为:

    00001-->赵辰 
    00002-->李为芳 
    00003-->王微

    logic:present

    如果指定的值出现该标签就会创建其标签体内的内容。该标签用于以下情况:

    • 检查具有指定名称的cookie是否出现。
    • 检查具有指定名称的header是否出现。
    • 检查具有指定名称的JSP Bean是否出现 或 检查具有指定名称的JSP Bean中的property属性是否出现。
    • 检查request中指定名称的参数是否出现。
    • 检查当前的认证用户是否与指定的安全角色相关联。
    • 检查当前认证的主体是否具有指定的名字。

    下面的代码示例了logic:present标签检查具有指定名称User-Agent的header是否出现:

    <logic:present header="User-Agent">
      您的浏览器是<bean:header name="User-Agent" id="userAgent"> <bean:write name="userAgent">。
    
    </bean:write>
    </bean:header></logic:present>

    logic:notPresent标签的应用正好和logic:present标签相反。

    logic:messagesPresent

    logic:messagesPresent标签用于以下情况:

    • 在request作用域中存在一个ActionMessages对象,标签的property属性和ActionMessages中的property对应。
    • 在request作用域中存在一个ActionErrors对象,标签的property属性和ActionErrors中的property对应。
    • 存在一个String对象,将其转换(构造)成ActionMessage然后在添加到ActionMessages中。
    • 存在一个String Array对象,将数组中的每一个String转换成一个ActionMessage,在将其添加到ActionMessages中。

    标签的message属性值为true时将以Globals.MESSAGE_KEY为key在request作用域中查找Message,其它情况下,将name的值作为key查找,如果name 没有出现,默认值为Globals.ERROR_KEY。

    下面的代码示例了logic:messagesPresent标签的典型用法:

    标签logic:messagesNotPresent的应用正好和logic:messagesPresent的应用相反。

    <logic:messagespresent>
      </logic:messagespresent>•  <html:messages id="message">
    • <bean:write name="message"></bean:write>
     </html:messages>

    logic:empty

    logic:empty标签是用来判断是否为空的。如果为空,该标签体中嵌入的内容就会被处理。该标签用于以下情况:

    • 当Java对象为null时
    • 当String对象为""时
    • 当java.util.Collection对象中的isEmpty()返回true时
    • 当java.util.Map对象中的isEmpty()返回true时

    下面的代码示例了logic:empty标签判断集合persons是否为空:

    <logic:empty property="persons" name="listForm">
      </logic:empty>


    集合persons为空!




    logic:notEmpty标签的应用正好和logic:empty标签相反。

    logic:match

    logic:match标签用来处理子串匹配问题。

    如果指定的值匹配该标签就会创建其标签体内的内容。该标签用于以下情况:

    • 检查具有指定名称的cookie是否与该标签的value匹配。
    • 检查具有指定名称的header是否与该标签的value匹配。
    • 检查具有指定名称的JSP Bean是否与该标签的value匹配 或 检查具有指定名称的JSP Bean中的property属性值是否与该标签的value匹配。
    • 检查request中指定名称的参数值是否与该标签的value匹配。

    下面的代码示例了logic:match标签的典型用法:

    <logic:present header="User-Agent">
      <logic:match value="MSIE 6.0" header="User-Agent">MS IE 6.0</logic:match>
    </logic:present>

    logic:notMatch标签的应用正好和logic:match标签相反。

    logic:equal

    这里要介绍的不只是logic:equal(=)标签,而是要介绍一类标签,这类标签完成比较运算,包括:

    • logic:equal(=)
    • logic:notEqual(!=)
    • logic:greaterEqual(>=)
    • logic:lessEqual(<=)
    • logic:graterThan(>)
    • logic:lessThan(<)

    该类标签的用法类似,我们只介绍logic:equal标签,其它的留给您自己去完成。

    logic:equal是用来判断是否相等的。如果相等,该标签体中嵌入的内容就会被处理。该标签用于以下情况:

    • 比较由该标签的cookie属性指定的cookie的值是否与该标签的value属性值相等。
    • 比较由该标签的header属性指定的header的值是否与该标签的value属性值相等。
    • 比较由该标签的name属性指定的JSP Bean是否与该标签的value属性值相等(property属性不出现)或比较由该标签的name属性指定的JSP Bean中的property属性值是否与该标签的value属性值相等。
    • 比较由该标签的parameter属性指定的参数值(request中)是否与该标签的value属性值相等。

    logic:forward

    我觉得将forward和redirect这两个动作放到一起对比着介绍更加有利于理解,基于此原因也就将logic:forward和logic:redirect这两个标签也拿到这里一起介绍了。

    让我们看看这两个动作的区别:

    • forward是在servlet内部执行,浏览器完全不会感知到这个动作,原始的url也不会改变,浏览器重新装载的话也只是对原始的请求进行简单的重复。
    • redirect则分成两个步骤:第一步是web应用程序告诉浏览器第二个url,然后浏览器向第二个url发送请求。

    redirect比forward慢,因为浏览器要做二次请求。还有就是要注意,在第一次的请求作用域(request作用域)内的bean对于第二次请求是不可见的。

    理 解了上面描述的区别也就知道了什么时候该选用logic:forward标签什么时候该选用logic:redirect标签了。logic: forward标签完成PageContext.forward()或HttpServletResponse.sendRedirect(),如何选择 由控制器决定。logic:redirect标签完成HttpServletResponse.sendRedirect()。

    在使用logic:redirect标签时我们可以向使用html:link一样构造baseurl和query参数。如果您感兴趣可以参考html:link标签。

    总结

    这 篇指南的背景是Struts-1.2.9,其中的所有的代码示例也都是在这个版本下调试通过的。目前Struts仍然在快速的发展中,但是从开发者应用的 角度来讲还是比较稳定的。因此我有理由相信其中的代码示例也可以很好的运行在其它的版本之上。最后希望这篇指南能够让您心情愉快!


    参考资料