<?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> <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>密 码:</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/> PreResultListener添加的数据:<s:property value="extra"/> </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.*; import com.opensymphony.xwork2.interceptor.PreResultListener; /** * 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 { ActionInvocation invocation = ActionContext .getContext().getActionInvocation(); invocation.addPreResultListener(new PreResultListener() { public void beforeResult(ActionInvocation invocation, String resultCode) { System.out.println("返回的逻辑视图名字为:" + resultCode); // 在返回Result之前加入一个额外的数据。 invocation.getInvocationContext().put("extra" , new java.util.Date() + "由" + resultCode + "逻辑视图名转入"); // 也可加入日志等 } }); if (getUsername().equals("crazyit.org") && getPassword().equals("leegang") ) { ActionContext.getContext().getSession() .put("user" , getUsername()); addActionMessage("欢迎," + getUsername() + ",您已经登录成功!"); return SUCCESS; } return ERROR; } }
<%-- 网站: <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="username"/> </body> </html>
<?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.devMode" value="true"/> <package name="lee" extends="struts-default"> <action name="login" class="org.crazyit.app.action.LoginAction"> <!-- 指定结果的类型为redirect, 这意味着系统该Action将重定向到welcome.jsp页面--> <result type="redirect">/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> <form action="login.action" method="post"> <table align="center"> <caption><h3>用户登录</h3></caption> <tr> <td>用户名:<input type="text" name="username"/></td> </tr> <tr align="center"> <td><input type="submit" value="登录"/><input type="reset" value="重填" /></td> </tr> </table> </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.devMode" value="true"/> <package name="lee" extends="struts-default"> <action name="login" class="org.crazyit.app.action.LoginAction"> <!-- 指定结果的类型为redirect, 这意味着系统该Action将重定向到welcome.jsp页面--> <result type="redirect">/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.*; import java.util.Map; /** * 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 { // 用于封装请求参数的username成员变量 private String username; // username的setter和getter方法 public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }