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

巧了我就是萌 提交于 2020-02-26 00:13:47
<?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"?>

<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"?>
<!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">
        <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="" %>
<%@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:append标签拼接集合和Map</title>
</head>
<body>
<!-- 使用append将List和Map集合拼接在一起
    新集合实际上是Map集合,其名字为newList -->
<s:append var="newList">
    <s:param value="#{'疯狂Java讲义':'李刚',
        '疯狂iOS讲义':'李刚',
        '经典Java EE企业应用实战':'李刚'}" />
    <s:param value="#{'http://www.crazyit.org', 
        'http://www.fkit.org'}" />
</s:append>
<table border="1" width="280">
<!-- 使用iterator迭代newList集合 -->
<s:iterator value="#newList" status="st">
    <tr <s:if test="#st.odd">
        style="background-color:#bbbbbb"</s:if>>
        <td><s:property value="key"/></td>
        <td><s:property value="value"/></td>
    </tr>
</s:iterator>
</table>
</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:generator生成集合</title>
</head>
<body>
<!-- 使用generator将一个字符串解析成一个集合
    ,指定了var和count属性 -->
<s:generator val="'疯狂Java讲义
    ,轻量级Java EE企业应用实战,
    疯狂iOS讲义'" separator=","
    var="books" count="2"/>
<table border="1" width="300">
<!-- 迭代输出Stack Congtext中的books集合 -->
<s:iterator value="#books">
    <tr>
        <td><s:property/></td>
    </tr>
</s:iterator>
</table>
${requestScope.books}
</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:generator生成集合</title>
</head>
<body>
<table border="1" width="240">
<!-- 使用generator标签将指定字符串解析成Iterator集合
    在generator标签内,得到的List集合位于ValueStack顶端 -->
<s:generator val="'疯狂Java讲义
    ,轻量级Java EE企业应用实战,
    疯狂iOS讲义'" separator=",">
<!-- 没有指定迭代哪个集合,直接迭代ValueStack顶端的集合 -->
<s:iterator status="st">
    <tr <s:if test="#st.odd">
        style="background-color:#bbbbbb"</s:if>>
        <td><s:property/></td>
    </tr>
</s:iterator>
</s:generator>
</table>
</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:if标签测试</title>
</head>
<body>
<!-- 在Stack Context中定义一个age属性,其值为29 -->
<s:set name="age" value="29"/>
<!-- 如果Stack Context中的age属性大于60 -->
<s:if test="#age>60">
    老年人
</s:if>
<!-- 如果Stack Context中的age属性大于35 -->
<s:elseif test="#age>35">
    中年人
</s:elseif>
<!-- 如果Stack Context中的age属性大于15 -->
<s:elseif test="#age>15">
    青年人
</s:elseif>
<s:else>
    少年
</s:else>
</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:itertor标签测试</title>
</head>
<body>
<table border="1" width="300">
<!-- 迭代输出List集合 -->
<s:iterator value="{'疯狂Java讲义',
    '轻量级Java EE企业应用实战',
    '疯狂iOS讲义'}"
    id="name" status="st">
    <tr <s:if test="#st.odd">
        style="background-color:#bbbbbb"</s:if>>
        <td><s:property value="name"/></td>
    </tr>
</s:iterator>
</table>
<table border="1" width="350">
    <tr>
        <th>书名</th>
        <th>作者</th>
    </tr>
<!-- 对指定的Map对象进行迭代输出,并指定status属性 -->
<s:iterator value="#{'疯狂Java讲义':'李刚',
    '轻量级Java EE企业应用实战':'李刚' ,
    '疯狂iOS讲义':'李刚'}"
    id="score" status="st">
    <!-- 根据当前被迭代元素的索引是否为奇数来决定是否使用背景色 -->
    <tr <s:if test="#st.odd">
        style="background-color:#bbbbbb"</s:if>>
        <!-- 输出Map对象里Entry的key -->
        <td><s:property value="key"/></td>
        <!-- 输出Map对象里Entry的value -->
        <td><s:property value="value"/></td>
    </tr>
</s:iterator>
</table>
</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:itertor标签迭代List</title>
</head>
<body>
<table border="1" width="300">
<s:iterator value="{'疯狂Java讲义',
    '轻量级Java EE企业应用实战',
    '疯狂iOS讲义'}"
    id="name">    
    <tr>
        <td><s:property value="name"/></td>
    </tr>
</s:iterator>
</table>
</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:merge标签迭代Map</title>
</head>
<body>
<s:merge id="newList">
    <s:param value="{'疯狂Java讲义',
        '轻量级Java EE企业应用实战',
        '疯狂iOS讲义'}" />
    <s:param value="{'http://www.crazyit.org', 
        'http://www.fkit.org'}" />
</s:merge>
<table border="1" width="240">
<s:iterator value="#newList" status="st">
    <tr <s:if test="#st.odd">style="background-color:#bbbbbb"</s:if>>
        <td><s:property/></td>
    </tr>
</s:iterator>
</table>
</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:merge标签迭代Map</title>
</head>
<body>
<s:merge id="newList">
    <s:param value="#{'疯狂Java讲义':'李刚',
        '轻量级Java EE企业应用实战':'李刚',
        '经典Java EE企业应用实战':'李刚'}" />
    <s:param value="#{'http://www.crazyit.org', 
        'http://blog.crazyit.org'}" />
</s:merge>

<table border="1" width="320">
<s:iterator value="#newList" status="st">
    <tr <s:if test="#st.odd">style="background-color:#bbbbbb"</s:if>>
        <td><s:property value="key"/></td>
        <td><s:property value="value"/></td>
    </tr>
</s:iterator>
</table>
</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:sort对集合元素进行排序</title>
</head>
<body>
<!-- 定义一个Comparator实例 -->
<s:bean var="mycomparator" name="org.crazyit.app.util.MyComparator"/>
<!-- 使用自定义的排序规则对目标集合进行排序 -->
<s:sort source="{'疯狂Java讲义'
    ,'轻量级Java EE企业应用实战'
    ,'经典Java EE企业应用实战'
    ,'疯狂Ajax讲义'
    ,'疯狂iOS讲义'}" 
    comparator="#mycomparator"
    var="sortedList"/>
输出page范围的sortedList属性:<br/>
${pageScope.sortedList}
<table border="1" width="300">
<!-- 迭代page范围内的sortedList属性 -->
<s:iterator status="st" value="#attr.sortedList">    
    <tr <s:if test="#st.odd">
        style="background-color:#bbbbbb"</s:if>>
        <td><s:property/></td>
    </tr>
</s:iterator>
</table>
</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:subset标签截取集合元素</title>
</head>
<body>
<table border="1" width="300">
<!-- 使用subset标签截取目标集合的4个元素,从第2个元素开始截取 -->
<s:subset source="{'疯狂Java讲义'
    ,'轻量级Java EE企业应用实战'
    ,'经典Java EE企业应用实战'
    ,'疯狂Ajax讲义'
    ,'疯狂iOS讲义'}" 
    start="1" count="4">
    <!-- 使用iterator标签来迭代目标集合,因为没有指定value属性值,
        故迭代ValueStack栈顶的集合 -->
    <s:iterator status="st">
        <!-- 根据当前迭代元素的索引是否为奇数决定是否使用CSS样式 -->
        <tr <s:if test="#st.odd">
            style="background-color:#bbbbbb"</s:if>>
            <td><s:property/></td>
        </tr>
    </s:iterator>
</s:subset>
</table>
</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:subset标签截取集合元素</title>
</head>
<body>
<!-- 定义一个Decider Bean -->
<s:bean var="mydecider" name="org.crazyit.app.util.MyDecider"/>
<!-- 使用自定义的Decider实例来截取目标集合,生成子集
    指定var属性,将生成的Itertor放入pageScope中 -->
<s:subset source="{'疯狂Java讲义'
    ,'轻量级Java EE企业应用实战'
    ,'经典Java EE企业应用实战'
    ,'疯狂Ajax讲义'
    ,'疯狂iOS讲义'}" 
    decider="#mydecider"
    var="newList"/>
直接输出page范围的newList属性:<br/>
${pageScope.newList}
<table border="1" width="240">
<!-- 迭代page范围内的newList属性 -->
<s:iterator status="st" value="#attr.newList">
    <tr <s:if test="#st.odd">
        style="background-color:#bbbbbb"</s:if>>
        <td><s:property/></td>
    </tr>
</s:iterator>
</table>
</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:append标签拼接两个集合</title>
</head>
<body>
<!-- 使用append标签将两个集合拼接成新的集合,
    新集合的名字是newList,新集合放入Stack Context中 -->
<s:append var="newList">
    <s:param value="{'疯狂Java讲义',
        '轻量级Java EE企业应用实战',
        '疯狂iOS讲义'}" />
    <s:param value="{'http://www.crazyit.org', 
        'http://www.fkit.org'}" />
</s:append>
<table border="1" width="260">
<!-- 使用iterator迭代newList集合 -->
<s:iterator value="#newList" status="st" id="ele">
    <tr>
        <td><s:property value="#st.count"/></td>
        <td><s:property value="ele"/></td>
    </tr>
</s:iterator>
</table>
</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">
        <action name="*">
            <result>/WEB-INF/content/{1}.jsp</result>
        </action>
    </package>

</struts>
package org.crazyit.app.util;

import java.util.Comparator;

/**
 * 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 MyComparator implements Comparator
{
    // 决定两个元素大小的方法
    public int compare(Object element1, Object element2)
    {
        // 根据元素字符串长度来决定大小
        return element1.toString().length()
            - element2.toString().length();
    }
}
package org.crazyit.app.util;

import org.apache.struts2.util.SubsetIteratorFilter;

/**
 * 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
 */
// 用户自定义的Decider类,实现了SubsetIteratorFilter.Decider接口
public class MyDecider
    implements SubsetIteratorFilter.Decider
{
    // 实现Decider接口必须实现的decide()方法,
    // 该方法决定集合中的元素是否被选入子集
    public boolean decide(Object element) throws Exception
    {
        String str = (String)element;
        // 如果集合元素(字符串)中包含Java EE子串,即可被选入子集
        return str.indexOf("Java EE") > 0;
    }
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!