常用的Struts 2.0的标志(Tag)介绍

来源:百度文库 编辑:神马文学网 时间:2024/04/29 22:49:58
常用的Struts 2.0的标志(Tag)介绍
在上一篇文章《为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 1.x Struts 2.0
分类 将标志库按功能分成HTML、Tiles、Logic和Bean等几部分 严格上来说,没有分类,所有标志都在URI为“/struts-tags”命名空间下,不过,我们可以从功能上将其分为两大类:非UI标志和UI标志
表达式语言(expression languages) 不支持嵌入语言(EL) OGNL、JSTL、Groovy和Velcity
以上表格,纯属个人总结,如有所不足或错误,请不吝指正
好了,我要开始介绍“常用”(这里所谓的“常用”,是指在已往工作中使用Struts里经常用到的)的标志了。
要在JSP中使用Struts 2.0标志,先要指明标志的引入。通过在JSP的代码的顶部加入以下代码可以做到这点。
<%@taglib prefix="s" uri="/struts-tags" %>
非UI标志 if、elseif和else
描述:
执行基本的条件流转。
参数:
名称 必需 默认 类型 描述 备注
test 是 Boolean 决定标志里内容是否显示的表达式 else标志没有这个参数
id 否 Object/String 用来标识元素的id。在UI和表单中为HTML的id属性
例子:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>



Condition Flow


Condition Flow



" />

Max‘s file here


Scott‘s file here


Other‘s file here



例1 condition.jsp
 
iterator
描述:
用于遍历集合(java.util.Collection)或枚举值(java.util.Iterator)。
参数:
名称 必需 默认 类型 描述
status 否 String 如果设置此参数,一个IteratorStatus的实例将会压入每个遍历的堆栈
value 否 Object/String 要遍历的可枚举的(iteratable)数据源,或者将放入新列表(List)的对象
id 否 Object/String 用来标识元素的id。在UI和表单中为HTML的id属性
例子:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<%
List list = new ArrayList();
list.add("Max");
list.add("Scott");
list.add("Jeffry");
list.add("Joe");
list.add("Kelvin");
request.setAttribute("names", list);
%>


Iterator


Names: 






  1. White 









例2 iterator.jsp
 
i18n
描述:
加载资源包到值堆栈。它可以允许text标志访问任何资源包的信息,而不只当前action相关联的资源包。
参数:
名称 必需 默认 类型 描述
value 是 Object/String 资源包的类路径(如com.xxxx.resources.AppMsg)
id 否 Object/String 用来标识元素的id。在UI和表单中为HTML的id属性
例子:
HelloWorld=Hello Wrold!
例3 classes\ ApplicationMessages.properties
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>



Internationization









例3 i18n.jsp
 
include
描述:
包含一个servlet的输出(servlet或jsp的页面)。
参数:
名称 必需 默认 类型 描述
value 是 String 要包含的jsp或servlet
id 否 Object/String 用来标识元素的id。在UI和表单中为HTML的id属性
例子:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>



Iterator


Interator Page



Max

i18n





例4 include.jsp
 
param
描述:
为其他标签提供参数,比如include标签和bean标签. 参数的name属性是可选的,如果提供,会调用Component的方法addParameter(String, Object), 如果不提供,则外层嵌套标签必须实现UnnamedParametric接口(如TextTag)。
value的提供有两种方式,通过value属性或者标签中间的text,不同之处我们看一下例子:
blue

(A)参数值会以String的格式放入statck.
(B)该值会以java.lang.Object的格式放入statck.
参数:
名称 必需 默认 类型 描述
name 否 String 参数名
value 否 String value表达式
id 否 Object/String 用来标识元素的id。在UI和表单中为HTML的id属性
例子:
请参考例4。
set
描述:
set标签赋予变量一个特定范围内的值。当希望给一个变量赋一个复杂的表达式,每次访问该变量而不是复杂的表达式时用到。其在两种情况下非常有用: 复杂的表达式很耗时 (性能提升) 或者很难理解 (代码可读性提高)。
参数:
名称 必需 默认 类型 描述
name 是 String 变量名字
scope 否 String 变量作用域,可以为application, session, request, page, 或action.
value 否 Object/String 将会赋给变量的值
id 否 Object/String 用来标识元素的id。在UI和表单中为HTML的id属性
例子:
请参考例1。
text
描述:
支持国际化信息的标签。国际化信息必须放在一个和当前action同名的resource bundle中,如果没有找到相应message,tag body将被当作默认message,如果没有tag body,message的name会被作为默认message。
参数:
名称 必需 默认 类型 描述
name 是 String 资源属性的名字
id 否 Object/String 用来标识元素的id。在UI和表单中为HTML的id属性
例子:
请参考例3。
url
描述:
该标签用于创建url,可以通过"param"标签提供request参数。
当includeParams的值时‘all‘或者‘get‘, param标签中定义的参数将有优先权,也就是说其会覆盖其他同名参数的值。
参数: 略
例子:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>



URL


URL


‘>i18n

Max

if\elseif\else


例5 url.jsp
 
property
描述:
得到‘value‘的属性,如果value没提供,默认为堆栈顶端的元素。
参数:
名称 必需 默认 类型 描述
default 否 String 如果属性是null则显示的default值
escape 否 true Booelean 是否escape HTML
value 否 栈顶 Object 要显示的值
id 否 Object/String 用来标识元素的id。在UI和表单中为HTML的id属性
例子:
请参考例2。
UI标志
UI标志又可以分为表单UI和非表单UI两部分。表单UI部分基本与Struts 1.x相同,都是对HTML表单元素的包装。不过,Struts 2.0加了几个我们经常在项目中用到的控件如:datepicker、doubleselect、timepicker、optiontransferselect等。因为这些标志很多都经常用到,而且参数也很多,要在一篇文章详细说明并非易事。
需要深入了解这些标志的朋友,可以到以下查看以下网址:
http://wiki.javascud.org/display/ww2cndoc/Tags WebWork2文档中文化计划(中文)
http://cwiki.apache.org/WW/tag-reference.html Tag Developers Guide(英文)
本文有相当的内容也来自这两处。
在此,我虽然不能够详细讲述这些标志,但是可以与大家分享一个来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();
    List vehicalTypeList = new ArrayList();
    Map vehicalSpecificMap = new HashMap();

    String thoughts;

    public UITagExample() {
        favouriteLanguages.add(new Language("EnglishKey", "English Language"));
        favouriteLanguages.add(new Language("FrenchKey", "French Language"));
        favouriteLanguages.add(new Language("SpanishKey", "Spanish Language"));

        VehicalType car = new VehicalType("CarKey", "Car");
        VehicalType motorcycle = new VehicalType("MotorcycleKey", "Motorcycle");
        vehicalTypeList.add(car);
        vehicalTypeList.add(motorcycle);

        List cars = new ArrayList();
        cars.add(new VehicalSpecific("MercedesKey", "Mercedes"));
        cars.add(new VehicalSpecific("HondaKey", "Honda"));
        cars.add(new VehicalSpecific("FordKey", "Ford"));

        List motorcycles = new ArrayList();
        motorcycles.add(new VehicalSpecific("SuzukiKey", "Suzuki"));
        motorcycles.add(new VehicalSpecific("YamahaKey", "Yamaha"));

        vehicalSpecificMap.put(car, cars);
        vehicalSpecificMap.put(motorcycle, motorcycles);
    }

    public List getLeftSideCartoonCharacters() {
        return leftSideCartoonCharacters;
    }
    public void setLeftSideCartoonCharacters(List leftSideCartoonCharacters) {
        this.leftSideCartoonCharacters = leftSideCartoonCharacters;
    }

    public List getRightSideCartoonCharacters() {
        return rightSideCartoonCharacters;
    }
    public void setRightSideCartoonCharacters(List rightSideCartoonCharacters) {
        this.rightSideCartoonCharacters = rightSideCartoonCharacters;
    }

    public String getFavouriteVehicalType() {
        return favouriteVehicalType;
    }

    public void setFavouriteVehicalType(String favouriteVehicalType) {
        this.favouriteVehicalType = favouriteVehicalType;
    }

    public String getFavouriteVehicalSpecific() {
        return favouriteVehicalSpecific;
    }

    public void setFavouriteVehicalSpecific(String favouriteVehicalSpecific) {
        this.favouriteVehicalSpecific = favouriteVehicalSpecific;
    }

    public List getVehicalTypeList() {
        return vehicalTypeList;
    }

    public List getVehicalSpecificList() {
        OgnlValueStack stack = ServletActionContext.getValueStack(ServletActionContext.getRequest());
        Object vehicalType = stack.findValue("top");
        if (vehicalType != null && vehicalType instanceof VehicalType) {
            List l = (List) vehicalSpecificMap.get(vehicalType);
            return l;
        }
        return Collections.EMPTY_LIST;
    }

    public List getFavouriteLanguages() {
        return favouriteLanguages;
    }

    public String execute() throws Exception {
        return SUCCESS;
    }

    /**//* Getters and Setters */

    public String doSubmit() {
        return SUCCESS;
    }

    // === inner class
    public static class Language {
        String description;
        String key;

        public Language(String key, String description) {
            this.key = key;
            this.description = description;
        }

        public String getKey() {
            return key;
        }
        public String getDescription() {
            return description;
        }

    }

    public static class VehicalType {
        String key;
        String description;
        public VehicalType(String key, String description) {
            this.key = key;
            this.description = description;
        }

        public String getKey() { return this.key; }
        public String getDescription() { return this.description; }

        public boolean equals(Object obj) {
            if (! (obj instanceof VehicalType)) {
                return false;
            }
            else {
                return key.equals(((VehicalType)obj).getKey());
            }
        }

        public int hashCode() {
            return key.hashCode();
        }
    }

    public static class VehicalSpecific {
        String key;
        String description;
        public VehicalSpecific(String key, String description) {
            this.key = key;
            this.description = description;
        }

        public String getKey() { return this.key; }
        public String getDescription() { return this.description; }

        public boolean equals(Object obj) {
            if (! (obj instanceof VehicalSpecific)) {
                return false;
            }
            else {
                return key.equals(((VehicalSpecific)obj).getKey());
            }
        }

        public int hashCode() {
            return key.hashCode();
        }
    }
}
例6 org.apache.struts2.showcase.UITagExample.java
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>


UI Tags Example







label="Name"
name="name"
tooltip="Enter your Name here" />
tooltip="Select Your Birthday"
label="Birthday"
name="birthday" />
tooltip="Enter your Biography"
label="Biograph"
name="bio"
cols="20"
rows="3"/>
tooltip="Choose Your Favourite Color"
label="Favorite Color"
list="{‘Red‘, ‘Blue‘, ‘Green‘}"
name="favoriteColor"
emptyOption="true"
headerKey="None"
headerValue="None"/>
tooltip="Choose Your Favourite Language"
label="Favourite Language"
list="favouriteLanguages"
name="favouriteLanguage"
listKey="key"
listValue="description"
emptyOption="true"
headerKey="None"
headerValue="None"/>
tooltip="Choose your Friends"
label="Friends"
list="{‘Patrick‘, ‘Jason‘, ‘Jay‘, ‘Toby‘, ‘Rene‘}"
name="friends"/>
tooltip="Confirmed that your are Over 18"
label="Age 18+"
name="legalAge"/>
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" />
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" />
tooltip="Upload Your Picture"
label="Picture"
name="picture" />
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" />
label="Your Thougths"
name="thoughts"
tooltip="Enter your thoughts here" />





例6 example.jsp

example.jsp
example.jsp

例6 struts.xml代码片段