struts2 이상 처리 (전역 및 국부 이상 정의)

7955 단어 exceptionstruts2
하나.struts2 국부 이상 처리
1.exception.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title> </title>
  </head>
  
  <body>
	<s:form action="exception.action" method="post">
		<s:textfield name="username" label="username"></s:textfield>
		<s:submit value="submit"></s:submit>
	</s:form>
  </body>
</html>

2.result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title> </title>
  </head>
  
  <body>
	username:${requestScope.username }
  </body>
</html>

3.usernameException.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title> </title>
  </head>
  
  <body>
	usernameException
  </body>
</html>

4.UsernameException.java(이상 클래스 정의)
package com.hitsoft.exception;

public class UsernameException extends Exception{
	private String message;
	public UsernameException(String message){
		super(message);
		this.message = message;
	}
	@Override
	public String getMessage() {
		return super.getMessage();
	}
	public void setMessage(String message) {
		this.message = message;
	}

}

5.ExceptionAction.java
package com.hitsoft.action;
import com.hitsoft.exception.UsernameException;
import com.opensymphony.xwork2.ActionSupport;

public class ExceptionAction extends ActionSupport{
	private String username;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	
	public String execute() throws Exception{
		if(!"hello".equals(username)){
			throw new UsernameException("username invalid!");
		}else{
			return "success";
		}	
	}
}

6.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">
	<!--   -->
    	<global-results>
    		<result name="usernameInvalid" type="redirect">/usernameException.jsp</result>
    	</global-results>
	<action name="exception" class="com.hitsoft.action.ExceptionAction">
	<!--   -->
		<exception-mapping result="usernameInvalid" exception="com.hitsoft.exception.UsernameException"></exception-mapping>
		<result name="success">/result.jsp</result>
		<result name="input">/exception.jsp</result>
	</action>
    </package>
</struts>

2.struts2 전역 이상 처리
1.exception.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title> </title>
  </head>
  
  <body>
	<s:form action="exception.action" method="post">
		<s:textfield name="password" label="password"></s:textfield>
		<s:submit value="submit"></s:submit>
	</s:form>
  </body>
</html>

2.result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title> </title>
  </head>
  
  <body>
	password:${requestScope.password }
  </body>
</html>

3.passwordException.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title> </title>
  </head>
  
  <body>
	passwordException
  </body>
</html>

4.PasswordException.java(이상 클래스 정의)

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


5.ExceptionAction.java

package com.hitsoft.action;
import com.hitsoft.exception.PasswordException;
import com.opensymphony.xwork2.ActionSupport;

public class ExceptionAction extends ActionSupport{
	private String password;
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String execute() throws Exception{
		if(!"world".equals(password)){
			throw new PasswordException("password invalid!");
		}else{
			return "success";
		}	
	}
}

6.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">
	<!--   -->
    	<global-results>
    		<result name="passwordInvalid" type="redirect">/passwordException.jsp</result>
    	</global-results>
    	<!--   -->
    	<global-exception-mappings>
    		<exception-mapping result="passwordInvalid" exception="com.hitsoft.exception.PasswordException"></exception-mapping>
    	</global-exception-mappings>
	<action name="exception" class="com.hitsoft.action.ExceptionAction">
		<result name="success">/result.jsp</result>
		<result name="input">/exception.jsp</result>
	</action>
    </package>
</struts>

좋은 웹페이지 즐겨찾기