吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:配置Action(1)

<?xml version="1.0" encoding="GBK"?>
<project name="struts" basedir="." default="">
    <property name="dist" value="classes"/>
    <property name="src" value="src"/>
    
    <path >
        <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 ref/>
        </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.devMode" value="true"/>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
    <package name="lee" extends="struts-default">
        <action name="login" class="org.crazyit.app.action.LoginRegistAction">
            <result name="error">/WEB-INF/content/error.jsp</result>
            <result>/WEB-INF/content/welcome.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>登录页面</title>
</head>
<body>
<table width="300" align="center">
<form action="login" method="post">
    <tr>
        <td>用户名:</td>
        <td><input type="text" name="username"/></td>
    </tr>
    <tr>
        <td>密&nbsp;&nbsp;码:</td>
        <td><input type="text" name="password"/></td>
    </tr>
    <tr>
        <td><input type="submit" value="登录"
            onclick="this.form.action='login';"/></td>
        <td><input type="submit" value="注册"
            onclick="regist();"/></td>
    </tr>
</form>
<table>
<script type="text/javascript">
function regist()
{
    // 获取页面的第一个表单
    targetForm = document.forms[0];
    // 动态修改表单的action属性
    targetForm.action = "login!regist";
}
</script>
</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>成功页面</title>
</head>
<body>
    <s:property value="tip"/>
</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.devMode" value="true"/>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
    <package name="lee" extends="struts-default">
        <action name="login" class="org.crazyit.app.action.LoginRegistAction">
            <result name="error">/WEB-INF/content/error.jsp</result>
            <result>/WEB-INF/content/welcome.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 LoginRegistAction
    extends ActionSupport
{
    // 封装用户请求参数的两个成员变量
    private String username;
    private String password;
    // 封装处理结果的tip成员变量
    private String tip;
    // username对应的setter和getter方法
    public String getUsername()
    {
        return username;
    }
    public void setUsername(String username)
    {
        this.username = username;
    }
    // password对应的getter和setter方法
    public String getPassword()
    {
        return password;
    }
    public void setPassword(String password)
    {
        this.password = password;
    }
    // tip对应的setter和getter方法
    public String getTip()
    {
        return tip;
    }
    public void setTip(String tip)
    {
        this.tip = tip;
    }
    // Action包含的注册控制逻辑
    public String regist() throws Exception
    {
        ActionContext.getContext().getSession()
            .put("user" , getUsername());
        setTip("恭喜您," + getUsername() + ",您已经注册成功!");
        return SUCCESS;
    }
    // Action默认包含的控制逻辑
    public String execute() throws Exception
    {
        if (getUsername().equals("crazyit.org")
            && getPassword().equals("leegang") )
        {
            ActionContext.getContext().getSession()
                .put("user" , getUsername());
            setTip("欢迎," + getUsername() + ",您已经登录成功!");
            return SUCCESS;
        }
        return ERROR;
    }
}
<?xml version="1.0" encoding="GBK"?>
<project name="struts" basedir="." default="">
    <property name="dist" value="classes"/>
    <property name="src" value="src"/>
    
    <path >
        <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 ref/>
        </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>
    <package name="lee" extends="struts-default">

        <!-- 配置name="*"的Action -->
        <action name="*" class="org.crazyit.app.action.FirstAction">
            <result>/WEB-INF/content/welcome.jsp</result>
        </action>
        <!-- 配置name="*Action"的Action -->
        <action name="*Action" class="org.crazyit.app.action.TwoAction">
            <result>/WEB-INF/content/welcome.jsp</result>
        </action>
        <!-- 配置name为loginAction的Action -->
        <action name="loginAction" class="org.crazyit.app.action.LoginAction">
            <result name="error">/WEB-INF/content/error.jsp</result>
            <result>/WEB-INF/content/welcome.jsp</result>
        </action>
    </package>
</struts>
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>

<html>
    <head>
        <title>错误页面</title>
    </head>
    <body>
        对不起,您登录失败!
    </body>
</html>
<%@ page language="java" contentType="text/html; charset=GBK"%>
<script>
    function regist()
    {
        targetForm = document.forms[0];
        targetForm.action = "registAction.action";
        targetForm.submit();
    }
</script>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<table width="300" align="center">
<form action="loginAction.action" method="post">
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>密&nbsp;&nbsp;码:</td>
<td><input type="text" name="password"/></td>
</tr>
<tr>
<td><input type="submit" value="登录"/></td>
<td><input type="button" value="注册" onClick="regist();"/></td>
</tr>
</form>
<table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
    <head>
        <title>成功页面</title>
    </head>
    <body>
    <s:actionmessage/>
    </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>
    <package name="lee" extends="struts-default">

        <!-- 配置name="*"的Action -->
        <action name="*" class="org.crazyit.app.action.FirstAction">
            <result>/WEB-INF/content/welcome.jsp</result>
        </action>
        <!-- 配置name="*Action"的Action -->
        <action name="*Action" class="org.crazyit.app.action.TwoAction">
            <result>/WEB-INF/content/welcome.jsp</result>
        </action>
        <!-- 配置name为loginAction的Action -->
        <action name="loginAction" class="org.crazyit.app.action.LoginAction">
            <result name="error">/WEB-INF/content/error.jsp</result>
            <result>/WEB-INF/content/welcome.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 FirstAction extends ActionSupport
{
    public String execute() throws Exception
    {
        addActionMessage("第一个Action处理结果");
        System.out.println("第一个Action");
        return SUCCESS;
    }
}
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 LoginAction extends ActionSupport
{
    private String username;
    private String password;

    public String getUsername()
    {
        return username;
    }
    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getPassword()
    {
        return password;
    }
    public void setPassword(String password)
    {
        this.password = password;
    }

    public String execute() throws Exception
    {
        if (getUsername().equals("crazyit.org")
                && getPassword().equals("leegang") )
        {
            ActionContext.getContext().getSession()
                .put("user" , getUsername());
            addActionMessage("欢迎," + getUsername() + ",您已经登录成功!");
            return SUCCESS;
        }
        return ERROR;
    }
}
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 TwoAction extends ActionSupport
{
    public String execute() throws Exception
    {
        addActionMessage("第二个Action处理结果");
        System.out.println("第二个Action");
        return SUCCESS;
    }
}
<?xml version="1.0" encoding="GBK"?>
<project name="struts" basedir="." default="">
    <property name="dist" value="classes"/>
    <property name="src" value="src"/>
    
    <path >
        <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 ref/>
        </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>
    <package name="lee" extends="struts-default">
        <!-- 配置login Action,处理类为LoginRegistAction
            默认使用execute方法处理请求-->
        <action name="login" class="org.crazyit.app.action.LoginRegistAction">
            <!-- 定义逻辑视图和物理视图之间的映射关系 -->
            <result name="error">/WEB-INF/content/error.jsp</result>
            <result>/WEB-INF/content/welcome.jsp</result>
        </action>
        <!-- 配置regist Action,处理类为LoginRegistAction
            指定使用regist方法处理请求-->
        <action name="regist" class="org.crazyit.app.action.LoginRegistAction"
            method="regist">
            <!-- 定义逻辑视图和物理视图之间的映射关系 -->
            <result name="error">/WEB-INF/content/error.jsp</result>
            <result>/WEB-INF/content/welcome.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>登录页面</title>
</head>
<body>
<table width="300" align="center">
<form action="login" method="post">
    <tr>
        <td>用户名:</td>
        <td><input type="text" name="username"/></td>
    </tr>
    <tr>
        <td>密&nbsp;&nbsp;码:</td>
        <td><input type="text" name="password"/></td>
    </tr>
    <tr>
        <td><input type="submit" value="登录"
            onclick="this.form.action='login';"/></td>
        <td><input type="submit" value="注册"
            onclick="regist();"/></td>
    </tr>
</form>
<table>
<script type="text/javascript">
function regist()
{
    // 获取页面的第一个表单
    targetForm = document.forms[0];
    // 动态修改表单的action属性
    targetForm.action = "regist";
}
</script>
</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>成功页面</title>
</head>
<body>
    <s:actionmessage/>
</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>
    <package name="lee" extends="struts-default">
        <!-- 配置login Action,处理类为LoginRegistAction
            默认使用execute方法处理请求-->
        <action name="login" class="org.crazyit.app.action.LoginRegistAction">
            <!-- 定义逻辑视图和物理视图之间的映射关系 -->
            <result name="error">/WEB-INF/content/error.jsp</result>
            <result>/WEB-INF/content/welcome.jsp</result>
        </action>
        <!-- 配置regist Action,处理类为LoginRegistAction
            指定使用regist方法处理请求-->
        <action name="regist" class="org.crazyit.app.action.LoginRegistAction"
            method="regist">
            <!-- 定义逻辑视图和物理视图之间的映射关系 -->
            <result name="error">/WEB-INF/content/error.jsp</result>
            <result>/WEB-INF/content/welcome.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 LoginRegistAction
    extends ActionSupport
{
    // 封装用户请求参数的两个成员变量
    private String username;
    private String password;
    // username的setter和getter方法
    public String getUsername()
    {
        return username;
    }
    public void setUsername(String username)
    {
        this.username = username;
    }
    // password的getter和setter方法
    public String getPassword()
    {
        return password;
    }
    public void setPassword(String password)
    {
        this.password = password;
    }
    // Action包含的注册控制逻辑
    public String regist() throws Exception
    {
        ActionContext.getContext().getSession()
            .put("user" , getUsername());
        addActionMessage("恭喜您," + getUsername() + ",您已经注册成功!");
        return SUCCESS;
    }
    // Action默认包含的控制逻辑
    public String execute() throws Exception
    {
        if (getUsername().equals("crazyit.org")
            && getPassword().equals("leegang") )
        {
            ActionContext.getContext().getSession()
                .put("user" , getUsername());
            addActionMessage("欢迎," + getUsername() + ",您已经登录成功!");
            return SUCCESS;
        }
        return ERROR;
    }
}
<?xml version="1.0" encoding="GBK"?>
<project name="struts" basedir="." default="">
    <property name="dist" value="classes"/>
    <property name="src" value="src"/>
    
    <path >
        <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 ref/>
        </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.devMode" value="true"/>
    <!-- 下面配置名为lee的包,该包继承了Struts 2的默认包
        没有指定命名空间,将使用默认命名空间 -->
    <package name="lee" extends="struts-default">
        <!-- 配置一个名为login的Action -->
        <action name="login" class="org.crazyit.app.action.LoginAction">
            <result name="error">/WEB-INF/content/error.jsp</result>
            <result>/WEB-INF/content/welcome.jsp</result>
        </action>
        <action name="*">
            <result>/WEB-INF/content/{1}.jsp</result>
        </action>
    </package>
    <!--下面配置名为book的包,该包继承了Struts 2的默认包。指定该包的命名空间为/book-->
    <package name="book" extends="struts-default" namespace="/book">
        <!-- 配置一个名为getBooks的Action -->
        <action name="getBooks" class="org.crazyit.app.action.GetBooksAction">
            <result name="login">/WEB-INF/content/loginForm.jsp</result>
            <result>/WEB-INF/content/showBook.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>错误提示页面</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>登录页面</title>
</head>
<body>
<s:form action="login">
    <s:textfield name="username" label="用户名"/>
    <s:textfield name="password" label="用户名"/>
    <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>图书信息</title>
</head>
<body>
<table border="1" width="360">
<caption>图书信息</caption>
<s:iterator value="books" status="index">
    <s:if test="#index.odd == true"> 
    <tr style="background-color:#cccccc">
    </s:if> 
    <s:else> 
    <tr>
    </s:else>
    <td>书名</td>
    <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>登录成功</title>
</head>
<body>
    欢迎登录:${sessionScope.user}<br/>
    <a href="book/getBooks.action">查看图书</a>
</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.devMode" value="true"/>
    <!-- 下面配置名为lee的包,该包继承了Struts 2的默认包
        没有指定命名空间,将使用默认命名空间 -->
    <package name="lee" extends="struts-default">
        <!-- 配置一个名为login的Action -->
        <action name="login" class="org.crazyit.app.action.LoginAction">
            <result name="error">/WEB-INF/content/error.jsp</result>
            <result>/WEB-INF/content/welcome.jsp</result>
        </action>
        <action name="*">
            <result>/WEB-INF/content/{1}.jsp</result>
        </action>
    </package>
    <!--下面配置名为book的包,该包继承了Struts 2的默认包。指定该包的命名空间为/book-->
    <package name="book" extends="struts-default" namespace="/book">
        <!-- 配置一个名为getBooks的Action -->
        <action name="getBooks" class="org.crazyit.app.action.GetBooksAction">
            <result name="login">/WEB-INF/content/loginForm.jsp</result>
            <result>/WEB-INF/content/showBook.jsp</result>
        </action>
    </package>
</struts>
package org.crazyit.app.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

import org.crazyit.app.service.*;
/**
 * 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 GetBooksAction
    implements Action
{
    private String[] books;

    public void setBooks(String[] books)
    {
        this.books = books;
    }
    public String[] getBooks()
    {
        return books;
    }

    public String execute() throws Exception
    {
        String user = (String)ActionContext.getContext()
            .getSession().get("user");
        if (user != null && user.equals("crazyit.org"))
        {
            BookService bs = new BookService();
            setBooks(bs.getLeeBooks());
            return SUCCESS;
        }
        return LOGIN;
    }
}
package org.crazyit.app.action;

import com.opensymphony.xwork2.*;

/**
 * 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 LoginAction extends ActionSupport
{
    private String username;
    private String password;

    // username的setter和getter方法
    public void setUsername(String username)
    {
        this.username = username;
    }
    public String getUsername()
    {
        return this.username;
    }

    // password的setter和getter方法
    public void setPassword(String password)
    {
        this.password = password;
    }
    public String getPassword()
    {
        return this.password;
    }

    public String execute() throws Exception
    {
        if (getUsername().equals("crazyit.org")
                && getPassword().equals("leegang") )
        {
            ActionContext.getContext().getSession()
                .put("user" , getUsername());
            return SUCCESS;
        }
        return ERROR;
    }

}
package org.crazyit.app.service;

/**
 * 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
{
    private String[] books =
        new String[]{
        "疯狂Java讲义" ,
        "轻量级Java EE企业应用实战",
        "疯狂iOS讲义",
        "疯狂Ajax讲义"
    };

    public String[] getLeeBooks()
    {
        return books;
    }
}