login_e.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<h2 class="men"><span>会员登录</span></h2>
<form id="login" method="post" action="login.action">
<div>
请输入用户名:
<input type="text" name="username" value="" class="txtBox" />
请输入密码
<input type="password" name="password" value="" class="txtBox" />
</div>
<div id="forget">忘记密码?
<input type="submit" name="login" value="登录" class="login" />
</div>
</form>
</body>
</html>
welcome.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
已经登录
<s:property value="model.tip"/>
</body>
</html>
exception.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<s:property value="exception.message"/>
</body>
</html>
error.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
错误
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>StrutsTutorial</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true"></constant>
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<!-- 引入默认文件(可能就是以前这里没有引入,所以错误啦) -->
<include file="struts-default.xml"></include>
<package name="mypackage" extends="struts-default">
<!-- 只要不带aciton限制的,都是global-results -->
<global-results>
<result name="sql">/exception.jsp</result>
<result name="root">/exception.jsp</result>
</global-results>
<!-- 当在java中抛出异常的时候,在这里扑捉 -->
<global-exception-mappings>
<exception-mapping result="sql" exception="java.sql.SQLException">
</exception-mapping>
<exception-mapping result="root" exception="java.lang.Exception">
</exception-mapping>
</global-exception-mappings>
<action name="login" class="lee.LoginAction">
<exception-mapping result="my" exception="lee.MyException">
</exception-mapping>
<result name="my">/exception.jsp</result>
<result name="success">/welcome.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
MyException.java
package lee;
public class MyException extends Exception
{
public MyException(){};
public MyException(String msg)
{
super(msg);
}
}
LoginAction.java
package lee;
import com.opensymphony.xwork2.Action;
public class LoginAction implements Action
{
private String username;
private String password;
private String tip;
public String getTip() {
return tip;
}
public void setTip(String tip) {
this.tip = tip;
}
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().equalsIgnoreCase("user"))
{
throw new MyException("自定义异常");
}
if(getUsername().equalsIgnoreCase("sql"))
{
throw new java.sql.SQLException("用户名不能为sql");
}
if(getUsername().equalsIgnoreCase("root"))
{
throw new java.sql.SQLException("用户名不能为root");
}
if(getUsername().equals("scott")&&getPassword().equals("tiger"))
{
setTip("服务器!");
return SUCCESS;
}
else {
return SUCCESS;
}
}
}
package lee;
import com.opensymphony.xwork2.Action;
public class LoginAction implements Action
{
private String username;
private String password;
private String tip;
public String getTip() {
return tip;
}
public void setTip(String tip) {
this.tip = tip;
}
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().equalsIgnoreCase("user"))
{
throw new MyException("自定义异常");
}
if(getUsername().equalsIgnoreCase("sql"))
{
throw new java.sql.SQLException("用户名不能为sql");
}
if(getUsername().equalsIgnoreCase("root"))
{
throw new java.sql.SQLException("用户名不能为root");
}
if(getUsername().equals("scott")&&getPassword().equals("tiger"))
{
setTip("服务器!");
return SUCCESS;
}
else {
return SUCCESS;
}
}
}