`

声名式异常处理

阅读更多

新建一个登录页面:

<body>
   <s:form action="login.action" method="POST">
   	<s:textfield name="username" label="Username"></s:textfield>
   	<s:textfield name="password" label="Password"></s:textfield>
   	<s:submit value="SUBMIT"></s:submit>
   </s:form>
  </body>

 

配置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="login" extends="struts-default" >
		<global-results>
			<result name="passwordInvalid">/passwordInvalid.jsp</result>
		</global-results>
		<global-exception-mappings><!-- 全局异常处理-->
			<exception-mapping result="passwordInvalid" exception="com.zchen.struts.exception.PasswordException"/>
		</global-exception-mappings>

		<action name="login" class="com.zchen.struts.action.LoginAction">
	<!-- 局部异常处理-->	<exception-mapping result="usernameInvalid" exception="com.zchen.struts.exception.UsernameException"></exception-mapping>
			<result name="success">/success.jsp</result>
			<result name="usernameInvalid">/usernameInvalid.jsp</result>
		</action>
		
	</package>
</struts>

 定义两个异常类:

package com.zchen.struts.exception;

public class UsernameException extends Exception {
	private static final long serialVersionUID = 6595905183933331564L;
	private String message;
	public UsernameException(String message){
		this.message = message;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
}

 

package com.zchen.struts.exception;

public class PasswordException extends Exception {
	private static final long serialVersionUID = 6595905183933331564L;
	private String message;
	public PasswordException(String message){
		this.message = message;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
}

 

定义Action类:

package com.zchen.struts.action;

import com.opensymphony.xwork2.ActionSupport;
import com.zchen.struts.exception.PasswordException;
import com.zchen.struts.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(this.getUsername().trim())){
			throw new UsernameException("username is invalid");
		}
		if(!"word".equals(this.getPassword().trim())){
			throw new PasswordException("password is invalid");
		}
		return super.execute();
	}
	
}

  新建跳转页面:

<body>
   <s:property value="exception.message"/>
 </body>

 

<body>
   <s:property value="exception.message"/>
</body>

 上面是自定的异常处理,我们也可以使用自带的:

 

局部异常处理:

<exception-mapping result="error" exception="java.sql.SQLException">

<result name="error">/error.jsp</result>

全局异常处理:

<global-exception-mapping>

<exception-mapping result="error" exception="java.lang.Exception">

</global-exception-mapping>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics