






















在上一篇文章《为Struts 2.0做好准备》中,我过于详细地介绍了Struts 2.0开发环境和运行环境的配置,所以,本文很少涉及的以上两方面的细节。如果,您看完《为Struts 2.0做好准备》后,还有什么不明白,或者没法运行文中例子,请联系我。我的E-MAIL:Max.M.Yuan@gmail.com。
在介绍常用标志前,我想先从总体上,对Struts 1.x与Struts 2.0的标志库(Tag Library)作比较。
以上表格,纯属个人总结,如有所不足或错误,请不吝指正
好了,我要开始介绍“常用”(这里所谓的“常用”,是指在已往工作中使用Struts里经常用到的)的标志了。
描述:
执行基本的条件流转。
参数:
例子:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Condition Flow</title>
</head>
<body>
<h3>Condition Flow</h3>
<!--
这里有点小技巧:
本来可以用#parameters.name[0]来获得,请求中name的值。但是,在我实现include例子时,
无论我用param标志给name赋任何值,#parameters里面不会含有任何值,所以#parameters.name也为空值。
其原因为:
当使用include标志时,被包含的页面(included)里#parameters拿到的是包含页面里的请求参数。
因此,这里必须手工调用request.getParameter("name")。
-->例1 condition.jsp
描述:
用于遍历集合(java.util.Collection)或枚举值(java.util.Iterator)。
参数:
例子:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
List list = new ArrayList();
list.add("Max");
list.add("Scott");
list.add("Jeffry");
list.add("Joe");
list.add("Kelvin");
request.setAttribute("names", list);
%>
<html>
<head>
<title>Iterator</title>
</head>
<body>
<h3>Names: </h3>
<!--
1、此处的空property元素用于获得当前iterator的值
2、status被设成stuts,在iterator的里面就可以通过#stuts取得IteratorStatus的对象。IteratorStatus类包含当前序号信息,如是否第一个或最后一个,是否为奇数序号。这些信息在我们做格式化的时候,显得非常有用。
-->
<ol>
<s:iterator value="#request.names" status="stuts">
<s:if test="#stuts.odd == true">
<li>White <s:property /></li>
</s:if>
<s:else>
<li style="background-color:gray"><s:property /></li>
</s:else>
</s:iterator>
</ol>
</body>
</html>
例2 iterator.jsp
描述:
加载资源包到值堆栈。它可以允许text标志访问任何资源包的信息,而不只当前action相关联的资源包。
参数:
例子:
HelloWorld=Hello Wrold!
例3 classes\ ApplicationMessages.properties
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Internationization</title>
</head>
<body>
<h3>
<s:i18n name="ApplicationMessages">
<s:text name="HelloWorld" />
</s:i18n>
</h3>
</body>
</html>
例3 i18n.jsp
描述:
包含一个servlet的输出(servlet或jsp的页面)。
参数:
例子:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Iterator</title>
</head>
<body>
<h3>Interator Page</h3>
<s:include value="/condition.jsp">
<s:param name="name">Max</s:param>
</s:include>
<h3>i18n</h3>
<s:include value="/i18n.jsp" />
</body>
</html>
例4 include.jsp
描述:
为其他标签提供参数,比如include标签和bean标签. 参数的name属性是可选的,如果提供,会调用Component的方法addParameter(String, Object), 如果不提供,则外层嵌套标签必须实现UnnamedParametric接口(如TextTag)。
参数:
例子:
请参考例4。
描述:
set标签赋予变量一个特定范围内的值。当希望给一个变量赋一个复杂的表达式,每次访问该变量而不是复杂的表达式时用到。其在两种情况下非常有用: 复杂的表达式很耗时 (性能提升) 或者很难理解 (代码可读性提高)。
参数:
例子:
请参考例1。
描述:
支持国际化信息的标签。国际化信息必须放在一个和当前action同名的resource bundle中,如果没有找到相应message,tag body将被当作默认message,如果没有tag body,message的name会被作为默认message。
参数:
例子:
请参考例3。
描述:
该标签用于创建url,可以通过"param"标签提供request参数。
参数: 略
例子:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>URL</title>
</head>
<body>
<h3>URL</h3>
<a href='<s:url value="/i18n.jsp" />'>i18n</a><br />
<s:url id="url" value="/condition.jsp">
<s:param name="name">Max</s:param>
</s:url>
<s:a href="%{url}">if\elseif\else</s:a>
</body>
</html>
例5 url.jsp
描述:
得到'value'的属性,如果value没提供,默认为堆栈顶端的元素。
参数:
例子:
请参考例2。
UI标志又可以分为表单UI和非表单UI两部分。表单UI部分基本与Struts 1.x相同,都是对HTML表单元素的包装。不过,Struts 2.0加了几个我们经常在项目中用到的控件如:datepicker、doubleselect、timepicker、optiontransferselect等。因为这些标志很多都经常用到,而且参数也很多,要在一篇文章详细说明并非易事。
在此,我虽然不能够详细讲述这些标志,但是可以与大家分享一个来Struts 2.0 Show Case一个例子。
/*
* $Id: UITagExample.java 420385 2006-07-10 00:57:05Z mrdon $
*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.struts2.showcase;import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Validateable;
import com.opensymphony.xwork2.util.OgnlValueStack;import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.File;/**
*/
public class UITagExample extends ActionSupport implements Validateable {private static final long serialVersionUID = -94044809860988047L;
String name;
Date birthday;
String bio;
String favoriteColor;
List friends;
boolean legalAge;
String state;
String region;
File picture;
String pictureContentType;
String pictureFileName;
String favouriteLanguage;
String favouriteVehicalType = "MotorcycleKey";
String favouriteVehicalSpecific = "YamahaKey";
List leftSideCartoonCharacters;
List rightSideCartoonCharacters;
List favouriteLanguages
= new ArrayList();String thoughts;
public UITagExample() {VehicalType car
= new VehicalType("CarKey", "Car");List cars
= new ArrayList();List motorcycles
= new ArrayList(); vehicalSpecificMap.put(car, cars);
vehicalSpecificMap.put(motorcycle, motorcycles);
}
}
public static class VehicalType {例6 org.apache.struts2.showcase.UITagExample.java
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>UI Tags Example</title>
<s:head/>
</head>
<body><s:actionerror/>
<s:actionmessage/>
<s:fielderror /><s:form action="exampleSubmit" method="post" enctype="multipart/form-data" tooltipConfig="#{'jsTooltipEnabled':'true'}">
<s:textfield
label="Name"
name="name"
tooltip="Enter your Name here" /><s:datepicker
tooltip="Select Your Birthday"
label="Birthday"
name="birthday" /><s:textarea
tooltip="Enter your Biography"
label="Biograph"
name="bio"
cols="20"
rows="3"/><s:select
tooltip="Choose Your Favourite Color"
label="Favorite Color"
list="{'Red', 'Blue', 'Green'}"
name="favoriteColor"
emptyOption="true"
headerKey="None"
headerValue="None"/><s:select
tooltip="Choose Your Favourite Language"
label="Favourite Language"
list="favouriteLanguages"
name="favouriteLanguage"
listKey="key"
listValue="description"
emptyOption="true"
headerKey="None"
headerValue="None"/><s:checkboxlist
tooltip="Choose your Friends"
label="Friends"
list="{'Patrick', 'Jason', 'Jay', 'Toby', 'Rene'}"
name="friends"/><s:checkbox
tooltip="Confirmed that your are Over 18"
label="Age 18+"
name="legalAge"/><s:doubleselect
tooltip="Choose Your State"
label="State"
name="region" list="{'North', 'South'}"
value="'South'"
doubleValue="'Florida'"
doubleList="top == 'North' ? {'Oregon', 'Washington'} : {'Texas', 'Florida'}"
doubleName="state"
headerKey="-1"
headerValue="---------- Please Select ----------"
emptyOption="true" /><s:doubleselect
tooltip="Choose your Vehical"
label="Favourite Vehical"
name="favouriteVehicalType"
list="vehicalTypeList"
listKey="key"
listValue="description"
value="'MotorcycleKey'"
doubleValue="'YamahaKey'"
doubleList="vehicalSpecificList"
doubleListKey="key"
doubleListValue="description"
doubleName="favouriteVehicalSpecific" headerKey="-1"
headerValue="---------- Please Select ----------"
emptyOption="true" /><s:file
tooltip="Upload Your Picture"
label="Picture"
name="picture" /><s:optiontransferselect
tooltip="Select Your Favourite Cartoon Characters"
label="Favourite Cartoons Characters"
name="leftSideCartoonCharacters"
leftTitle="Left Title"
rightTitle="Right Title"
list="{'Popeye', 'He-Man', 'Spiderman'}"
multiple="true"
headerKey="headerKey"
headerValue="--- Please Select ---"
emptyOption="true"
doubleList="{'Superman', 'Mickey Mouse', 'Donald Duck'}"
doubleName="rightSideCartoonCharacters"
doubleHeaderKey="doubleHeaderKey"
doubleHeaderValue="--- Please Select ---"
doubleEmptyOption="true"
doubleMultiple="true" /><s:textarea
label="Your Thougths"
name="thoughts"
tooltip="Enter your thoughts here" /><s:submit onclick="alert('aaaa');" />
<s:reset onclick="alert('bbbb');" />
</s:form></body>
</html>
例6 example.jsp
<action name="example" class="org.apache.struts2.showcase.UITagExample">
<result>example.jsp</result>
<result name="input">example.jsp</result>
</action>
例6 struts.xml代码片段
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。