<?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"> <!-- 定义全局结果映射 --> <global-results> <!-- 定义当sql、root两个逻辑异常都对应exception.jsp页 --> <result name="sql">/WEB-INF/content/exception.jsp</result> <result name="root">/WEB-INF/content/exception.jsp</result> </global-results> <!-- 定义全局异常映射 --> <global-exception-mappings> <!-- 当Action中遇到SQLException异常时, 系统将转入name为sql的结果中--> <exception-mapping exception="java.sql.SQLException" result="sql"/> <!-- 当Action中遇到Exception异常时, 系统将转入name为root的结果中--> <exception-mapping exception="java.lang.Exception" result="root"/> </global-exception-mappings> <action name="login" class="org.crazyit.app.action.LoginAction"> <!-- 定义局部异常映射, 当Action中遇到MyException异常时, 系统将转入name为my的结果中--> <exception-mapping exception="org.crazyit.app.exception.MyException" result="my"/> <!-- 定义三个结果映射 --> <result name="my">/WEB-INF/content/exception.jsp</result> <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="" %> <%@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:property value="exception.message"/> </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> <form action="login.action" method="post"> <table align="center"> <caption><h3>用户登录</h3></caption> <tr> <td>用户名:<input type="text" name="username"/></td> </tr> <tr> <td>密 码:<input type="text" name="password"/></td> </tr> <tr align="center"> <td><input type="submit" value="登录"/><input type="reset" value="重填" /></td> </tr> </table> </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> <s:property value="username"/>,您已经登录!<br/> <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"> <!-- 定义全局结果映射 --> <global-results> <!-- 定义当sql、root两个逻辑异常都对应exception.jsp页 --> <result name="sql">/WEB-INF/content/exception.jsp</result> <result name="root">/WEB-INF/content/exception.jsp</result> </global-results> <!-- 定义全局异常映射 --> <global-exception-mappings> <!-- 当Action中遇到SQLException异常时, 系统将转入name为sql的结果中--> <exception-mapping exception="java.sql.SQLException" result="sql"/> <!-- 当Action中遇到Exception异常时, 系统将转入name为root的结果中--> <exception-mapping exception="java.lang.Exception" result="root"/> </global-exception-mappings> <action name="login" class="org.crazyit.app.action.LoginAction"> <!-- 定义局部异常映射, 当Action中遇到MyException异常时, 系统将转入name为my的结果中--> <exception-mapping exception="org.crazyit.app.exception.MyException" result="my"/> <!-- 定义三个结果映射 --> <result name="my">/WEB-INF/content/exception.jsp</result> <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 org.crazyit.app.exception.*; /** * 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和password成员变量 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().equalsIgnoreCase("user")) { throw new MyException("自定义异常"); } if (getUsername().equalsIgnoreCase("sql")) { throw new java.sql.SQLException("用户名不能为SQL"); } if (getUsername().equals("crazyit.org") && getPassword().equals("leegang") ) { addActionMessage("哈哈,服务器提示!"); return SUCCESS; } return ERROR; } }
package org.crazyit.app.exception; /** * 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 MyException extends Exception { public MyException() { } public MyException(String msg) { super(msg); } }