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>

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值