吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:使用Struts2的标签库--表单标签

<?xml version="1.0" encoding="GBK"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

    <!-- 定义Struts 2的核心Filter -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <!-- 让Struts 2的核心Filter拦截所有请求 -->
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>
<?xml version="1.0" encoding="GBK"?>
<project name="struts" basedir="." default="">
    <property name="dist" value="classes"/>
    <property name="src" value="src"/>
    
    <path id="classpath">
        <fileset dir="lib">
            <include name="*.jar"/>
        </fileset>
        <pathelement path="${dist}"/>
    </path>

    <target name="compile" description="Compile all source code">
        <delete dir="${dist}"/>
        <mkdir dir="${dist}"/>
        <copy todir="${dist}">
            <fileset dir="${src}">
                <exclude name="**/*.java"/>
            </fileset>        
        </copy>
        <javac destdir="classes" debug="true" includeantruntime="yes"
            deprecation="false" optimize="false" failonerror="true">
            <src path="${src}"/>
            <classpath refid="classpath"/>
        </javac>
    </target>

</project>
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="GBK"/>
    <package name="lee" extends="struts-default">

        <!-- 定义名为pro的Action,其实现类为ProAction -->
        <action name="pro" class="org.crazyit.app.action.ProAction">
            <!-- 使用系统默认的拦截器栈 -->
            <interceptor-ref name="defaultStack"/>
            <!-- 使用防刷新的token拦截器 -->
            <interceptor-ref name="token"/>
            <!-- 定义重复提交转向的视图,该逻辑视图名必须是invalid.token -->
            <result name="invalid.token">/WEB-INF/content/refresh.jsp</result>
            <!-- 如果处理结果返回success,对应/show.jsp视图资源 -->
            <result>/WEB-INF/content/show.jsp</result>
        </action>
        <action name="*">
            <result>/WEB-INF/content/{1}.jsp</result>
        </action>
    </package>
</struts>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>防刷新测试</title>
</head>
<body>
您的请求已被处理!请不要刷新页面
</body>
</html>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>使用s:checkboxlist生成多个复选框</title>
    <s:head/>
</head>
<body>
<h3>使用s:checkboxlist生成多个复选框</h3>
<s:form>
<!-- 使用简单集合来生成多个复选框 -->
<s:checkboxlist name="a" label="请选择您喜欢的图书" 
    labelposition="top" list="{'轻量级Java EE企业应用实战'
    , '疯狂iOS讲义'
    , '疯狂Java讲义'}"/>
<!-- 使用简单Map对象来生成多个复选框
    使用Map对象的key(书名)作为复选框的value,
    使用Map对象的value(出版时间)作为复选框的标签-->
<s:checkboxlist name="b" label="请选择您想选择出版日期" 
    labelposition="top"    list="#{'疯狂Java讲义':'2008年9月'
    ,'轻量级Java EE企业应用实战':'2008月12月'
    ,'疯狂iOS讲义':'2014年1月'}"
    listKey="key"
    listValue="value"/>
<!-- 创建一个JavaBean对象,并将其放入Stack Context中 -->
<s:bean name="org.crazyit.app.service.BookService" id="bs"/>
<!-- 使用集合里放多个JavaBean实例来生成多个复选框
    使用集合元素里name属性作为复选框的标签
    使用集合元素里author属性作为复选框的value-->
<s:checkboxlist name="b" label="请选择您喜欢的图书" 
    labelposition="top"
    list="#bs.books"
    listKey="author"
    listValue="name"/>
</s:form>
</body>
</html>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>使用s:doubleselect生成级联下拉列表框</title>
    <s:head/>
</head>
<body>
<h3>使用s:doubleselect生成级联下拉列表框</h3>
<s:form action="x">
    <s:doubleselect
        label="请选择您喜欢的图书"
        name="author" list="{'李刚', 'David'}"
        doubleList="top == '李刚' ? {'轻量级Java EE企业应用实战',
        '疯狂iOS讲义','疯狂Java讲义'}:
        {'JavaScript: The Definitive Guide'}" 
        doubleName="book"/>    
</s:form>
</body>
</html>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>使用s:doubleselect生成级联下拉列表框</title>
    <s:head/>
</head>
<body>
<h3>使用s:doubleselect生成级联下拉列表框</h3>
<!-- 创建一个复杂的Map对象,key为普通字符串,value为集合 -->
<s:set name="bs" value="#{'李刚': {'疯狂Java讲义', 
    '轻量级Java EE企业应用实战','疯狂iOS讲义'},
    'David': {'JavaScript: The Definitive Guide'},
    'Johnson': {'Expert One-on-One J2EE Design and Development'}}"/>
<!-- 使用Map对象来生成级联列表框 -->
<s:form action="x">
    <s:doubleselect
        label="请选择您喜欢的图书"
        size="3"
        name="author" list="#bs.keySet()"
        doubleList="#bs[top]" 
        doubleSize="3"
        doubleName="book"/>    
</s:form>
</body>
</html>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>防刷新测试</title>
</head>
<body>
    用户正常提交,提交的书名为:${requestScope.book}
</body>
</html>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>使用s:optgroup生成下拉选择框的选项组</title>
    <s:head/>
</head>
<body>
<h3>使用s:optgroup生成下拉选择框的选项组</h3>
<s:form>
<!-- 直接使用Map为列表框生成选项 -->
<s:select label="选择您喜欢的图书" name="book" size="7"
    list="#{'疯狂Java讲义':'李刚'
        ,'轻量级Java EE企业应用实战':'李刚'
        ,'疯狂iOS讲义':'李刚'}"
    listKey="value"
    listValue="key">
    <!-- 使用Map对象来生成选择框的选项组 -->
    <s:optgroup label="Rod Johnson"
        list="#{'Expert One-on-One J2EE Design and Development':'Johnson'}"
        listKey="value"
        listValue="key"/>
    <s:optgroup label="David Flanagan"
        list="#{'JavaScript: The Definitive Guide':'David'}"
        listKey="value"
        listValue="key"/>
</s:select>
</s:form>
</body>
</html>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>使用s:optiontransferselect来生成可移动列表项的下拉列表框</title>
    <s:head/>
</head>
<body>
<h3>使用s:optiontransferselect来生成可移动列表项的下拉列表框</h3>
<s:form>
<!-- 使用简单集合对象来生成可移动的下拉列表框 -->
 <s:optiontransferselect 
      label="请选择你喜欢的图书"
    name="cnbook" 
    leftTitle="中文图书:"
    rightTitle="外文图书"
    list="{'疯狂Java讲义' ,'疯狂iOS讲义',
        '轻量级Java EE企业应用实战','经典Java EE企业应用实战'}" 
    multiple="true"
    addToLeftLabel="向左移动"
    selectAllLabel="全部选择"
    addAllToRightLabel="全部右移"
    headerKey="cnKey"
    headerValue="--- 选择中文图书 ---"
    emptyOption="true"
    doubleList="{'Expert One-on-One J2EE Design and Development',
        'JavaScript: The Definitive Guide'}" 
    doubleName="enBook"
    doubleHeaderKey="enKey"
    doubleHeaderValue="--- 选择外文图书 ---" 
    doubleEmptyOption="true"
    doubleMultiple="true"
/>
</s:form>
</body>
</html>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>使用s:radio生成多个单选框</title>
    <s:head/>
</head>
<body>
<h3>使用s:radio生成多个单选框</h3>
<s:form>
<!-- 使用简单集合来生成多个单选框 -->
<s:radio name="a" label="请选择您喜欢的图书" labelposition="top"
    list="{'疯狂Java讲义','轻量级Java EE企业应用实战',
        '疯狂iOS讲义'}"/>
<!-- 使用简单Map对象来生成多个单选框 -->
<s:radio name="b" label="请选择您想选择出版日期" labelposition="top"
    list="#{'疯狂Java讲义':'2008年9月'
    ,'轻量级Java EE企业应用实战':'2008月12月'
    ,'疯狂iOS讲义':'2014年1月'}"
    listKey="key"
    listValue="value"/>
<!-- 创建一个JavaBean实例 -->
<s:bean name="org.crazyit.app.service.BookService" id="bs"/>
<!-- 使用集合里放多个JavaBean实例来生成多个单选框 -->
<s:radio name="c" label="请选择您喜欢的图书" labelposition="top"
    list="#bs.books"
    listKey="author"
    listValue="name"/>
</s:form>
</body>
</html>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>使用s:select生成下拉选择框</title>
    <s:head/>
</head>
<body>
<h3>使用s:select生成下拉选择框</h3>
<s:form>
<!-- 使用简单集合来生成下拉选择框 -->
<s:select name="a" label="请选择您喜欢的图书" labelposition="top" 
    multiple="true" list="{'疯狂Java讲义','轻量级Java EE企业应用实战',
    'JavaScript: The Definitive Guide'}"/>
<!-- 使用简单Map对象来生成下拉选择框 -->
<s:select name="b" label="请选择您想选择出版日期" labelposition="top" 
    list="#{'疯狂Java讲义':'2008年9月',
    '轻量级Java EE企业应用实战':'2008月12月', 
    '疯狂iOS讲义':'2014年1月'}"
    listKey="key"
    listValue="value"/>
<!-- 创建一个JavaBean实例 -->
<s:bean name="org.crazyit.app.service.BookService" id="bs"/>
<!-- 使用集合里放多个JavaBean实例来生成下拉选择框 -->
<s:select name="c" label="请选择您喜欢的图书" labelposition="top"
    multiple="true"
    list="#bs.books"
    listKey="author"
    listValue="name"/>
</s:form>
</body>
</html>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>使用s:token防止重复提交</title>
</head>
<body>
<h3>使用s:token防止重复提交</h3>
<s:form action="pro">
    <!-- 普通表单域 -->
    <s:textfield name="book" label="书名"/>
    <!-- 用于防刷新的token -->
    <s:token/>
    <s:submit value="提交"/>
</s:form>
</body>
</html>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>使用s:updownselect生成可上下移动选项的下拉选择框</title>
    <s:head/>
</head>
<body>
<h3>使用s:updownselect生成可上下移动选项的下拉选择框</h3>
<s:form>
<!-- 使用简单集合来生成可上下移动选项的下拉选择框 -->
<s:updownselect name="a" label="请选择您喜欢的图书"
    labelposition="top"
    moveUpLabel="向上移动"
    list="{'疯狂Java讲义' 
    , '轻量级Java EE企业应用实战'
    , '疯狂iOS讲义'}"/>
<!-- 使用简单Map对象来生成可上下移动选项的下拉选择框 
     且使用emptyOption="true"增加一个空选项-->
<s:updownselect name="b" label="请选择您想选择出版日期"
    labelposition="top"
    moveDownLabel="向下移动"
    list="#{'疯狂Java讲义':'2008年9月'
    ,'轻量级Java EE企业应用实战':'2008月12月'
    ,'疯狂iOS讲义':'2014年1月'}"
    listKey="key"
    emptyOption="true"
    listValue="value"/>
<s:bean name="org.crazyit.app.service.BookService" id="bs"/>
<!-- 使用集合里放多个JavaBean实例来可上下移动选项的生成下拉选择框 -->
<s:updownselect name="c" label="请选择您喜欢的图书的作者"
    labelposition="top"    selectAllLabel="全部选择" multiple="true"
    list="#bs.books"
    listKey="author"
    listValue="name"/>
</s:form>
</body>
</html>
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="GBK"/>
    <package name="lee" extends="struts-default">

        <!-- 定义名为pro的Action,其实现类为ProAction -->
        <action name="pro" class="org.crazyit.app.action.ProAction">
            <!-- 使用系统默认的拦截器栈 -->
            <interceptor-ref name="defaultStack"/>
            <!-- 使用防刷新的token拦截器 -->
            <interceptor-ref name="token"/>
            <!-- 定义重复提交转向的视图,该逻辑视图名必须是invalid.token -->
            <result name="invalid.token">/WEB-INF/content/refresh.jsp</result>
            <!-- 如果处理结果返回success,对应/show.jsp视图资源 -->
            <result>/WEB-INF/content/show.jsp</result>
        </action>
        <action name="*">
            <result>/WEB-INF/content/{1}.jsp</result>
        </action>
    </package>
</struts>
package org.crazyit.app.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;

/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class ProAction extends ActionSupport
{
    private String book;

    // book的setter和getter方法
    public void setBook(String book)
    {
        this.book = book;
    }
    public String getBook()
    {
        return this.book;
    }
}
package org.crazyit.app.dto;

/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class Book
{
    private String name;
    private String author;

    // 无参数的构造器
    public Book()
    {
    }
    // 初始化全部成员变量的构造器
    public Book(String name , String author)
    {
        this.name = name;
        this.author = author;
    }

    // name的setter和getter方法
    public void setName(String name)
    {
        this.name = name;
    }
    public String getName()
    {
        return this.name;
    }

    // author的setter和getter方法
    public void setAuthor(String author)
    {
        this.author = author;
    }
    public String getAuthor()
    {
        return this.author;
    }

}
package org.crazyit.app.service;

import org.crazyit.app.dto.*;
/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class BookService
{
    public Book[] getBooks()
    {
        return new Book[]
        {
            new Book("疯狂Java讲义","李刚"),
            new Book("轻量级Java EE企业应用实战","李刚"),
            new Book("疯狂iOS讲义","李刚"),
            new Book("疯狂Ajax讲义","李刚")
        };
    }
}
原文地址:https://www.cnblogs.com/tszr/p/12364760.html