异常处理机制
程序员文章站
2024-03-24 23:35:34
...
1. 定义异常类
1.1 UsernameException.java
1.2 PasswordException.java
2. 定义异常页面
2.1 usernameInvalid.jsp
2.2 passwordInvalid.jsp
3. LoginAction.java
4. Struts.xml
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>