struts2异常处理(全局与局部异常定义)
一.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="username" label="username"></s:textfield><s:submit value="submit"></s:submit></s:form> </body></html>
<%@ 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>
<%@ 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>
package com.hitsoft.exception;public class UsernameException extends Exception{private String message;public UsernameException(String message){super(message);this.message = message;}@Overridepublic String getMessage() {return super.getMessage();}public void setMessage(String message) {this.message = message;}}
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";}}}
<?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" exception="com.hitsoft.exception.UsernameException"></exception-mapping><result name="success">/result.jsp</result><result name="input">/exception.jsp</result></action> </package></struts>
<%@ 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>
<%@ 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>
<%@ 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>
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;}}
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";}}}
<?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>