Struts HTML 标签

用来创建能够和Struts 框架和其他相应的HTML 标签交互的HTML 输入表单

  1. html:base
  2. html:cancel
  3. html:select
  4. html:checkbox
  5. html:radio
  6. html:multibox
  7. html:link
  8. html:errors
  9. html:javascript
  10. html:xhtml


 

html:base

html:cancel

html:select

Name

Required

Default

Evaluated

Type

Description

accesskey false   false String Set the html accesskey attribute on rendered html element
cssClass false   false String The css class to use for element
cssStyle false   false String The css style definitions for element to use
disabled false   false String Set the html disabled attribute on rendered html element
emptyOption false false false Boolean Whether or not to add an empty (--) option after the header option
headerKey false   false String Key for first item in list. Must not be empty! '-1' and '' is correct, '' is bad.
headerValue false   false String Value expression for first item in list
id false   false String HTML id attribute
javascriptTooltip false false false Boolean Use JavaScript to generate tooltips
key false   false String Set the key (name, value, label) for this particular component
label false   false String Label expression used for rendering a element specific label
labelSeparator false : false String String that will be appended to the labe
labelposition false   false String Define label position of form element (top/left)
list true   false String Iterable source to populate from. If the list is a Map (key, value), the Map key will become the option 'value' parameter and the Map value will become the option body.
listKey false   false String Property of list objects to get field value from
listValue false   false String Property of list objects to get field content from
multiple false false false Boolean Creates a multiple select. The tag will pre-select multiple values if the values are passed as an Array or a Collection(of appropriate types) via the value attribute. If one of the keys equals one of the values in the Collection or Array it wil be selected
name false   false String The name to set for element
onblur false   false String Set the html onblur attribute on rendered html element
onchange false   false String Set the html onchange attribute on rendered html element
onclick false   false String Set the html onclick attribute on rendered html element
ondblclick false   false String Set the html ondblclick attribute on rendered html element
onfocus false   false String Set the html onfocus attribute on rendered html element
onkeydown false   false String Set the html onkeydown attribute on rendered html element
onkeypress false   false String Set the html onkeypress attribute on rendered html element
onkeyup false   false String Set the html onkeyup attribute on rendered html element
onmousedown false   false String Set the html onmousedown attribute on rendered html element
onmousemove false   false String Set the html onmousemove attribute on rendered html element
onmouseout false   false String Set the html onmouseout attribute on rendered html element
onmouseover false   false String Set the html onmouseover attribute on rendered html element
onmouseup false   false String Set the html onmouseup attribute on rendered html element
onselect false   false String Set the html onselect attribute on rendered html element
required false false false Boolean If set to true, the rendered element will indicate that input is required
requiredposition false   false String Define required position of required form element (left|right)
size false   false Integer Size of the element box (# of elements to show)
tabindex false   false String Set the html tabindex attribute on rendered html element
template false   false String The template (other than default) to use for rendering the element
templateDir false   false String The template directory.
theme false   false String The theme (other than default) to use for rendering the element
title false   false String Set the html title attribute on rendered html element
tooltip false   false String Set the tooltip of this particular component
tooltipConfig false   false String Deprecated. Use individual tooltip configuration attributes instead.
tooltipCssClass false StrutsTTClassic false String CSS class applied to JavaScrip tooltips
tooltipDelay false Classic false String Delay in milliseconds, before showing JavaScript tooltips
tooltipIconPath false   false String Icon path used for image that will have the tooltip
value false   false String Preset the value of input element.

html:checkbox

Name

Required

Default

Evaluated

Type

Description

accesskey false   false String Set the html accesskey attribute on rendered html element
cssClass false   false String The css class to use for element
cssStyle false   false String The css style definitions for element to use
disabled false   false String Set the html disabled attribute on rendered html element
fieldValue false true false String The actual HTML value attribute of the checkbox.
id false   false String HTML id attribute
javascriptTooltip false false false Boolean Use JavaScript to generate tooltips
key false   false String Set the key (name, value, label) for this particular component
label false   false String Label expression used for rendering a element specific label
labelSeparator false : false String String that will be appended to the labe
labelposition false   false String Define label position of form element (top/left)
name false   false String The name to set for element
onblur false   false String Set the html onblur attribute on rendered html element
onchange false   false String Set the html onchange attribute on rendered html element
onclick false   false String Set the html onclick attribute on rendered html element
ondblclick false   false String Set the html ondblclick attribute on rendered html element
onfocus false   false String Set the html onfocus attribute on rendered html element
onkeydown false   false String Set the html onkeydown attribute on rendered html element
onkeypress false   false String Set the html onkeypress attribute on rendered html element
onkeyup false   false String Set the html onkeyup attribute on rendered html element
onmousedown false   false String Set the html onmousedown attribute on rendered html element
onmousemove false   false String Set the html onmousemove attribute on rendered html element
onmouseout false   false String Set the html onmouseout attribute on rendered html element
onmouseover false   false String Set the html onmouseover attribute on rendered html element
onmouseup false   false String Set the html onmouseup attribute on rendered html element
onselect false   false String Set the html onselect attribute on rendered html element
required false false false Boolean If set to true, the rendered element will indicate that input is required
requiredposition false   false String Define required position of required form element (left|right)
tabindex false   false String Set the html tabindex attribute on rendered html element
template false   false String The template (other than default) to use for rendering the element
templateDir false   false String The template directory.
theme false   false String The theme (other than default) to use for rendering the element
title false   false String Set the html title attribute on rendered html element
tooltip false   false String Set the tooltip of this particular component
tooltipConfig false   false String Deprecated. Use individual tooltip configuration attributes instead.
tooltipCssClass false StrutsTTClassic false String CSS class applied to JavaScrip tooltips
tooltipDelay false Classic false String Delay in milliseconds, before showing JavaScript tooltips
tooltipIconPath false   false String Icon path used for image that will have the tooltip
value false   false String Preset the value of input element.

html:radio

 

Name

Required

Default

Evaluated

Type

Description

accesskey false   false String Set the html accesskey attribute on rendered html element
cssClass false   false String The css class to use for element
cssStyle false   false String The css style definitions for element to use
disabled false   false String Set the html disabled attribute on rendered html element
id false   false String HTML id attribute
javascriptTooltip false false false Boolean Use JavaScript to generate tooltips
key false   false String Set the key (name, value, label) for this particular component
label false   false String Label expression used for rendering a element specific label
labelSeparator false : false String String that will be appended to the labe
labelposition false   false String Define label position of form element (top/left)
list true   false String Iterable source to populate from. If the list is a Map (key, value), the Map key will become the option 'value' parameter and the Map value will become the option body.
listKey false   false String Property of list objects to get field value from
listValue false   false String Property of list objects to get field content from
name false   false String The name to set for element
onblur false   false String Set the html onblur attribute on rendered html element
onchange false   false String Set the html onchange attribute on rendered html element
onclick false   false String Set the html onclick attribute on rendered html element
ondblclick false   false String Set the html ondblclick attribute on rendered html element
onfocus false   false String Set the html onfocus attribute on rendered html element
onkeydown false   false String Set the html onkeydown attribute on rendered html element
onkeypress false   false String Set the html onkeypress attribute on rendered html element
onkeyup false   false String Set the html onkeyup attribute on rendered html element
onmousedown false   false String Set the html onmousedown attribute on rendered html element
onmousemove false   false String Set the html onmousemove attribute on rendered html element
onmouseout false   false String Set the html onmouseout attribute on rendered html element
onmouseover false   false String Set the html onmouseover attribute on rendered html element
onmouseup false   false String Set the html onmouseup attribute on rendered html element
onselect false   false String Set the html onselect attribute on rendered html element
required false false false Boolean If set to true, the rendered element will indicate that input is required
requiredposition false   false String Define required position of required form element (left|right)
tabindex false   false String Set the html tabindex attribute on rendered html element
template false   false String The template (other than default) to use for rendering the element
templateDir false   false String The template directory.
theme false   false String The theme (other than default) to use for rendering the element
title false   false String Set the html title attribute on rendered html element
tooltip false   false String Set the tooltip of this particular component
tooltipConfig false   false String Deprecated. Use individual tooltip configuration attributes instead.
tooltipCssClass false StrutsTTClassic false String CSS class applied to JavaScrip tooltips
tooltipDelay false Classic false String Delay in milliseconds, before showing JavaScript tooltips
tooltipIconPath false   false String Icon path used for image that will have the tooltip
value false   false String Preset the value of input element.

html:multibox

html:link

html:errors

html:javascript

html:xhtml

 

 


html:base
html:cancel
html:select
html:checkbox
html:radio
html:multibox
html:link
html:errors
html:javascript
html:xhtml
logic:iterate
logic:present
logic:messagesPresent
logic:empty
logic:match
logic:equal
logic:forward

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 id="jSession" name="JSESSIONID"/>
  <!-- 其它标签通过绑定到page作用域中的属性使用该值 -->
  这个cookie的名称是<bean:write name="jSession" property="name"/>,
  值为<bean:write name="jSession" property="value"/>。<br/>
  <!-- JSP脚本通过scripting变量使用该值 -->
  <%
    String name = jSession.getName();
    String value = jSession.getValue();
    out.println("这个cookie的名称是"+name+",值为"+value+"。
    <br/>");	
  %>
</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 id="values" name="listForm" property = "persons" 
	type="java.util.List"/> 

下面给出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;
  }
} 

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

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

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

<%
  Person p = new Person();
  for(int i=0;i<values.size();i++){
    p = (Person)values.get(i);
    out.println(p.getId());
    out.println("<br/>");
  }		
%>

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">
  <!-- 其它标签通过绑定到page作用域中的属性使用该值 -->
  您的浏览器是<bean:header id="userAgent" name="User-Agent"/>
  <bean:write name="userAgent"/>。<br/>
  <!-- JSP脚本通过scripting变量使用该值 -->
  <%
    out.println("您的浏览器是"+userAgent+"。<br/>");	
  %>
</logic:present>

bean:include

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

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

<bean:include id="value" page="/include.txt"/>
<!-- 其它标签通过绑定到page作用域中的属性使用该值 -->
<bean:write name="value"/><br/>
<!-- JSP脚本通过scripting变量使用该值 -->
<%
  out.println(value);	
%>

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文件,三个文件的名字分别为Resources.properties、Resources_en.properties和Resources_zh.properties。在struts-config.xml文件中的设置(这里不用设置三个,struts会依据locale自动找到对应的文件)如下:

<message-resources parameter="Resources" />

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

<!-- Resources.properties -->
resource=Resources.properties.
from=Resources.properties.
<!-- Resources_en.properties -->
from=Resources_en.properties.
<!-- Resources_zh.properties 
  因为文件的编码被限制为ISO8859所以要有汉字必须用jdk的native2ascii提前转换 
-->
from=Resources_zh.properties.

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

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

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

<form-bean name="localeForm" 
    type="org.apache.struts.action.DynaActionForm">
  <form-property name="language" type="java.lang.String" />
  <form-property name="country" type="java.lang.String" />
  <!--action成功后要跳到那里-->
  <form-property name="page"  type="java.lang.String" 
    initial="/message.jsp"/>
</form-bean>
	
<action path="/locale" type="org.apache.struts.actions.LocaleAction"
  name="localeForm" scope="request">
</action>

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

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

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

bean:page

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

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

<bean:page id="res" property="response"/>
<!-- 其它标签通过绑定到page作用域中的属性使用该值 -->
<bean:write name="res" property="characterEncoding"/><br/>
<bean:write name="res" property="contentType"/><br/>
<!-- JSP脚本通过scripting变量使用该值 -->
<%
  String characterEncoding = res.getCharacterEncoding();
  String contentType = res.getContentType();
  out.println(characterEncoding+"<br/>");	
  out.println(contentType+"<br/>");	
%>

您可以用和上面类似的代码访问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 id="p" name="param"/>
<bean:write name="p"/>
<bean:parameter id="ps" multiple="true" name="param"/>
<logic:iterate id="p" name="ps">
  <bean:write name="p"/><br/>
</logic:iterate>

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 id="str" name="/resource.txt"/>
<!-- 其它标签通过绑定到page作用域中的属性使用该值 -->
<bean:write name="str"/><br/>
<!-- JSP脚本通过scripting变量使用该值 -->
<%
  out.println(str+"<br/>");
%>
<bean:resource id="is" input="true" name="/resource.txt"/>
<!-- 其它标签通过绑定到page作用域中的属性使用该值 -->
<bean:write name="is"/><br/>
<bean:write name="is" property="class"/>
<!-- JSP脚本通过scripting变量使用该值 -->
<%
  out.println(is+"<br/>");
  out.println(is.getClass()+"<br/>");
%>

bean:size

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

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

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

bean:struts

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

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

<!-- listForm的定义<form-bean name="listForm" 
  type="org.solo.struts.form.ListForm" /> -->
<bean:struts id="listFormBean" formBean="listForm"/>
name:<bean:write name="listFormBean" property="name"/><br/>
type:<bean:write name="listFormBean" property="type"/><br/>
dynamic:<bean:write name="listFormBean" property="dynamic"/><br/>

上面代码运行的结果为:

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 id="header" name="User-Agent"/>
  <bean:write name="header"/>
</logic:present>

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

<bean:define id="date" name="dataForm" property="now"/>
<br/><bean:write name="date"/>
<br/><bean:write name="date" format="MM/dd/yyyy"/>
<br/><bean:write name="date" formatKey="format.date.standard"/>

上面代码运行的结果为:

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元素对照表
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 property="cancellable" value="true"/>这一行,如果不添加Struts会抛出org.apache.struts.action.InvalidCancelException异常。这是我在完成本指南的过程中发现的唯一向下不兼容的地方。

<action path="/cancel" 
  type="org.solo.struts.action.CancelAction" name="cancelForm" 
    scope="request">
  <set-property property="cancellable" value="true"/>
  <forward name="success" path="/cancel.jsp" />
</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 id="personCollection" name="selectForm" 
  property="persons"/>
<html:select name="selectForm" property="person.id" size="1">
  <html:options collection="personCollection" property="id" 
    labelProperty="name"/> 
</html:select>

没有指定collection属性

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

  private List<String> ids = null;
  private List<String> names = null;

上面的代码来自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 name="selectForm" property="person.id" size="1">
  <html:options property="ids" labelProperty="names"/> 
</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 name="selectForm" property="person.id" size="1">
  <html:optionsCollection name="selectForm" property="persons" 
  label="name" value="id"/> 
</html:select>

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

<html:select name="selectForm" property="personIds" 
    multiple="true" size="2">
  <html:options property="ids" labelProperty="names"/> 
</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 name="checkboxForm" property="one">
  One
</html:checkbox>
<html:checkbox name="checkboxForm" property="two">
  Two
</html:checkbox>
<html:checkbox name="checkboxForm" property="three">
  Three
</html:checkbox>

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

html:radio

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

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

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

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

<logic:notEmpty name="radioForm" property="persons">		
  <logic:iterate id="person" name="radioForm" property="persons">
    <html:radio idName="person" property="id" value="id">
      <bean:write name="person" property="name"/>
    </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 name="multiboxForm" property="selectedItems" 
  value="00001"/>
<html:multibox name="multiboxForm" property="selectedItems" 
  value="00002"/>

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

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

html:link

html:link标签生成一个锚点(<a>)元素。我从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");
}
<!-- 这用来设置一个位置 -->
<html:link linkName="top"/>
<!-- 这用来定位到上面的那个位置 -->
<html:link page="/link.do" anchor="top">Go Top</html:link>
<html:link page="/link.do" paramId="p" paramName="dataForm" 
    paramProperty="param">
  单参单值
</html:link><br/>
<html:link page="/link.do" name="dataForm" property="paramSingleMap">
  多参单值
</html:link><br/>
<html:link page="/link.do" name="dataForm" property="paramMultiMap">
  单参多值
</html:link><br/>
<html:link page="/link.do" name="dataForm" property="paramSMMap">
  混合
</html:link>

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

<logic:iterate id="person" name="dataForm" property="persons">
  <html:link action="/link.do" paramId="person" paramName="person" 
	  paramProperty="id" indexed="true" indexId="number">
    person
  </html:link>
  <br/>
</logic:iterate>
<!-- 下面是上面代码的运行结果(产生的html)
  <a href="/struts-demo/link.do?person=00001&number=0">赵辰</a><br/>
  <a href="/struts-demo/link.do?person=00002&number=1">李为芳</a><br/>
  <a href="/struts-demo/link.do?person=00003&number=2">王微</a><br/> 
  其中的number是由indexId="number"确定的,而该参数的值为元素在集合中的位置。
-->

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

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=<ul>
errors.prefix=<li>
errors.suffix=</li>
errors.footer=</ul>

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/>
<br/>	
<html:errors property="property4"/>

messages标签代码示例:

<logic:messagesPresent>
  <ul>
  <html:messages id="message">
    <li><bean:write name="message"/></li>
  </html:messages>
  </ul>
</logic:messagesPresent>
<br/>
<logic:messagesPresent>
  <ul>
  <html:messages id="message" property="property4">
    <li><bean:write name="message"/></li>
  </html:messages>
</ul>
</logic:messagesPresent>

 

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

 

html:javascript

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

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

<html:javascript formName="dataForm"/>

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:iterate叠代ArrayList的示例的对象引用关系和部分代码:

 

图示 3. logic:iterate中对象的引用关系
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");
}

标签输出的结果为:

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 id="userAgent" name="User-Agent"/>
    <bean:write name="userAgent"/>。<br/>
</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:messagesPresent>
  <ul>
  <html:messages id="message">
    <li><bean:write name="message"/></li>
  </html:messages>
  </ul>
</logic:messagesPresent>	

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

logic:empty

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

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

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

<logic:empty name="listForm" property = "persons">
  <div>集合persons为空!</div>			
</logic:empty> 	

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 header="User-Agent" value="MSIE 6.0">
    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标签。