我将代码贴出来
目录结构
自定义异常:
PasswordException.java
package exception;public class PasswordException extends Exception { private static final long serialVersionUID = 8194779832109312113L; 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; } }
UsernameException.java源代码
package exception;public class UsernameException extends Exception { private static final long serialVersionUID = 5926837471651227661L; 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; } }
Login.java源代码
package test;import com.opensymphony.xwork2.ActionSupport;import exception.PasswordException;import exception.UsernameException;public class Login extends ActionSupport{ private static final long serialVersionUID = 1L; 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(this.getUsername().trim()) ){ throw new UsernameException( "username" ); } else if ( !"world".equals(this.getPassword().trim()) ){ throw new PasswordException( "password" ); }else{ return SUCCESS; } // if ( "hello".equals(this.getUsername().trim()) && "world".equals(this.getPassword().trim())){// return "success";// } else {// this.addFieldError( "username" , "用户名或密码错误,请重新输入。" );// this.setUsername( "" );// this.setPassword( "" );// return "failer";// } }// public void validate() {// if ( (null == this.getUsername()) || ("".equals(this.getUsername().trim())) ){// this.addFieldError( "username" , "用户名不能为空。" );// }// if ( (null == this.getPassword()) || ("".equals(this.getPassword().trim())) ){// this.addFieldError( "password" , "密码不能为空。" );// }// } }
sturts.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> <constant name="struts.custom.i18n.resources" value="message"> </constant> <package name="struts2" extends="struts-default"> <action name="login" class="test.Login"> <exception-mapping result="username" exception="test.UsernameException"> </exception-mapping> <result name="username">/username.jsp</result> <exception-mapping result="password" exception="test.PasswordException"> </exception-mapping> <result name="password">/password.jsp</result> <result name="success">/result.jsp</result> </action> </package></struts>
web.xml源代码
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>