81,092
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
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;
}
}
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" , "密码不能为空。" );
// }
// }
}
<?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>
<?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>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>
<s:form action="login">
<s:textfield name="username" label="用户名:"></s:textfield>
<s:password name="password" label="密码:"></s:password>
<s:submit ></s:submit>
</s:form>
</body>
</html>
if ( !"hello".equals(this.getUsername().trim()) ){
throw new UsernameException( "username" ); //这里抛出异常后后面不再执行,而struts捕获的就是你这里抛出的这个异常错误
} else if ( !"world".equals(this.getPassword().trim()) ){
throw new PasswordException( "password" );
}else{
return SUCCESS;
}