一.struts2局部异常处理
1.exception.jsp
2.result.jsp
3.usernameException.jsp
4.UsernameException.java(异常类定义)
5.ExceptionAction.java
6.struts.xml
二.struts2全局异常处理
1.exception.jsp
2.result.jsp
3.passwordException.jsp
4.PasswordException.java(异常类定义)
5.ExceptionAction.java
6.struts.xml
1.exception.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>局部异常测试</title>
</head>
<body>
<s:form action="exception.action" method="post">
<s:textfield name="username" label="username"></s:textfield>
<s:submit value="submit"></s:submit>
</s:form>
</body>
</html>
2.result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>局部异常成功页面</title>
</head>
<body>
username:${requestScope.username }
</body>
</html>
3.usernameException.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>局部异常页面</title>
</head>
<body>
usernameException
</body>
</html>
4.UsernameException.java(异常类定义)
package com.hitsoft.exception;
public class UsernameException extends Exception{
private String message;
public UsernameException(String message){
super(message);
this.message = message;
}
@Override
public String getMessage() {
return super.getMessage();
}
public void setMessage(String message) {
this.message = message;
}
}
5.ExceptionAction.java
package com.hitsoft.action;
import com.hitsoft.exception.UsernameException;
import com.opensymphony.xwork2.ActionSupport;
public class ExceptionAction extends ActionSupport{
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String execute() throws Exception{
if(!"hello".equals(username)){
throw new UsernameException("username invalid!");
}else{
return "success";
}
}
}
6.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>
<package name="struts2" extends="struts-default">
<!-- 全局结果 -->
<global-results>
<result name="usernameInvalid" type="redirect">/usernameException.jsp</result>
</global-results>
<action name="exception" class="com.hitsoft.action.ExceptionAction">
<!-- 局部异常映射 -->
<exception-mapping result="usernameInvalid" exception="com.hitsoft.exception.UsernameException"></exception-mapping>
<result name="success">/result.jsp</result>
<result name="input">/exception.jsp</result>
</action>
</package>
</struts>
二.struts2全局异常处理
1.exception.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>全局异常测试</title>
</head>
<body>
<s:form action="exception.action" method="post">
<s:textfield name="password" label="password"></s:textfield>
<s:submit value="submit"></s:submit>
</s:form>
</body>
</html>
2.result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>全局异常成功页面</title>
</head>
<body>
password:${requestScope.password }
</body>
</html>
3.passwordException.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>全局异常页面</title>
</head>
<body>
passwordException
</body>
</html>
4.PasswordException.java(异常类定义)
package com.hitsoft.exception;
public class PasswordException extends Exception{
private String message;
public PasswordException(String message){
super(message);
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
5.ExceptionAction.java
package com.hitsoft.action;
import com.hitsoft.exception.PasswordException;
import com.opensymphony.xwork2.ActionSupport;
public class ExceptionAction extends ActionSupport{
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() throws Exception{
if(!"world".equals(password)){
throw new PasswordException("password invalid!");
}else{
return "success";
}
}
}
6.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>
<package name="struts2" extends="struts-default">
<!-- 全局结果 -->
<global-results>
<result name="passwordInvalid" type="redirect">/passwordException.jsp</result>
</global-results>
<!-- 全局异常映射 -->
<global-exception-mappings>
<exception-mapping result="passwordInvalid" exception="com.hitsoft.exception.PasswordException"></exception-mapping>
</global-exception-mappings>
<action name="exception" class="com.hitsoft.action.ExceptionAction">
<result name="success">/result.jsp</result>
<result name="input">/exception.jsp</result>
</action>
</package>
</struts>