异常处理机制

1. 定义异常类
1.1 UsernameException.java

package com.test.exception;

public class UsernameException extends Exception {

private String message;

public UsernameException(String message){
super(message);
this.message=message;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}

1.2 PasswordException.java

package com.test.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;
}

}

2. 定义异常页面
2.1 usernameInvalid.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
username invalid.
</body>
</html>

2.2 passwordInvalid.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
password invalid.
</body>
</html>

3. LoginAction.java

package com.test.action;

import com.opensymphony.xwork2.ActionSupport;
import com.test.exception.PasswordException;
import com.test.exception.UsernameException;

public class LoginAction extends ActionSupport{

private String username;
private String password;

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(!"hello".equals(username)){
throw new UsernameException("username invalid");
}else if(!"world".equals(password)){
throw new PasswordException("password invalid");
}else{
return SUCCESS;
}
}

@Override
public void validate() {
// if(username!=null&&-1!=username.indexOf("hello")){
// this.addFieldError("username", "username invalid");
// }
// if(password!=null&&password.length()<4){
// this.addFieldError("password", "password invalid");
// }
}
}

4. 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">

<interceptors>
<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor">
<param name="hello">world</param>
</interceptor>
<interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2">
</interceptor>
<interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3">
</interceptor>
<interceptor name="authInterceptor" class="com.test.interceptor.AuthInterceptor">
</interceptor>

<interceptor-stack name="myStack">
<interceptor-ref name="authInterceptor"></interceptor-ref>
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="myInterceptor2"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>

<global-results>
<result name="login" type="redirect">/login.jsp</result>
<result name="usernameInvalid" type="redirect">/usernameInvalid.jsp</result>
<result name="passwordInvalid" type="redirect">/passwordInvalid.jsp</result>
</global-results>
<!--
<default-interceptor-ref name="myStack"></default-interceptor-ref>
-->
<action name="helloworld" class="com.test.action.HelloWorld">
<result>/helloworld.jsp</result>
</action>
注:可以定义成全局异常信息
<global-exception-mappings>
<exception-mapping result="usernameInvalid" exception="com.test.exception.UsernameException"></exception-mapping>
<exception-mapping result="passwordInvalid" exception="com.test.exception.PasswordException"></exception-mapping>
</global-exception-mappings>
<action name="login" class="com.test.action.LoginAction">
<exception-mapping result="usernameInvalid" exception="com.test.exception.UsernameException"></exception-mapping>
<exception-mapping result="passwordInvalid" exception="com.test.exception.PasswordException"></exception-mapping>
<result>/result.jsp</result>
<result name="input">/login2.jsp</result>
</action>

<action name="converterAction" class="com.test.action.PointAction" method="test" >
<result name="success">/output.jsp</result>
<result name="input">/error.jsp</result>
</action>

<action name="register" class="com.test.action.RegisterAction">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>
<!--
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="myInterceptor2"></interceptor-ref>
-->
<interceptor-ref name="myStack"></interceptor-ref>


</action>

</package>
</struts>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值