1. 程式人生 > >struts中異常處理

struts中異常處理

RuntimeExceptionException的區別------

Exception需要顯式捕獲

Struts中的異常處理和國際化訊息文字密切聯絡

Struts中處理異常的三種方式:

1.程式設計式異常

Action中捕獲異常

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

UserForm formbean = (UserForm)form;

UserManager userManager = new UserManagerImpl();

try {

userManager.login(formbean.getUsername(), formbean.getPassword());

} catch (Exception e) {

e.printStackTrace();

//建立國際化訊息文字

ActionMessages errors = new ActionMessages();

ActionMessage error = new ActionMessage("errors.detail",e.getMessage());

errors.add("user.exception", error);

//傳遞國際化訊息文字

this.saveErrors(request, errors);

return mapping.findForward("error");

}

return mapping.findForward("success");

}

頁面顯示異常資訊

<body>

<html:messages id="msg" message="true">

${msg}

</html:messages>

<html:errors/>

</body>

注意:如果處理的是messages物件,需要新增屬性message="true"

2.

宣告式異常

只需要在struts配置檔案中配置

<action path="/login"

type="cn.yjj.test.web.UserAction"

name="userForm"

scope="request"

input="/login_error.jsp"

>

<exception key="errors.detail" type="java.lang.RuntimeException" path="/login_error.jsp"/>

<forward name="success" path="/login_success.jsp"/>

<forward name="error" path="/login_error.jsp"/>

</action>

Key----國際化訊息文字中的key

Type----異常類的完整路徑

Path-----轉向路徑優先順序>input屬性

3.個性化異常處理(解決多個國際化文字和填充符的問題)

需要了解struts中自動處理異常的機制

異常類

package cn.yjj.test.base;

publicclass SystemException extends RuntimeException {

private String errorCode;

private Object[] params;

public SystemException(String message){

super(message);

}

public SystemException(String message, String errorCode){

this(message, errorCode, null);

}

public SystemException(String message, String errorCode, Object param){

this(message, errorCode, new Object[]{param});

}

public SystemException(String message, String errorCode, Object[] params){

super(message);

this.errorCode = errorCode;

this.params = params;

}

public String getErrorCode() {

returnerrorCode;

}

public Object[] getParams() {

returnparams;

}

}

異常處理類

package cn.yjj.test.base;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.Globals;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionMessage;

import org.apache.struts.action.ExceptionHandler;

import org.apache.struts.config.ExceptionConfig;

import org.apache.struts.util.ModuleException;

publicclass SystemExceptionHandler extends ExceptionHandler {

public ActionForward execute(

Exception ex,

ExceptionConfig ae,

ActionMapping mapping,

ActionForm formInstance,

HttpServletRequest request,

HttpServletResponse response)

throws ServletException {

if(!(ex instanceof SystemException)){

returnsuper.execute(ex, ae, mapping, formInstance, request, response);

}

ActionForward forward = null;

ActionMessage error = null;

String property = null;

// Build the forward from the exception mapping if it exists

// or from the form input

if (ae.getPath() != null) {

forward = new ActionForward(ae.getPath());

} else {

forward = mapping.getInputForward();

}

// Figure out the error

if (ex instanceof ModuleException) {

error = ((ModuleException) ex).getActionMessage();

property = ((ModuleException) ex).getProperty();

} else {

SystemException se = (SystemException)ex;

String errorCode = se.getErrorCode();

Object[] params = se.getParams();

if(errorCode==null){

error = new ActionMessage(ae.getKey(), se.getMessage());

}else{

error = new ActionMessage(errorCode, params);

}

property = error.getKey();

}

this.logException(ex);

// Store the exception

request.setAttribute(Globals.EXCEPTION_KEY, ex);

this.storeException(request, property, error, forward, ae.getScope());

return forward;

}

}

配置檔案

<action path="/login"

type="cn.yjj.test.web.UserAction"

name="userForm"

scope="request"

input="/login_error.jsp"

>

<exception key="errors.detail" type="cn.yjj.test.base.SystemException"

path="/login_error.jsp" handler="cn.yjj.test.base.SystemExceptionHandler"/>

<forward name="success" path="/login_success.jsp"/>

<forward name="error" path="/login_error.jsp"/>

</action>

1、程式設計式異常

* 截獲異常

* 建立相應的異常訊息

* 傳遞異常訊息

* 轉向相應的頁面處理異常

2、宣告式異常(自動處理的異常)

* struts-config.xml檔案中配置<exeception/>標籤

* 理解區域性和全域性exception

* 注意區域性<exception/>標籤需要配置到<forward/>標籤的前面,詳見dtd中的約束

<exeception/>標籤中的屬性說明:

* key:指異常資訊對應的國際化訊息文字,這個key值需要在國際化資原始檔中定義

* type: 處理那種異常

* path: 定義一但出現異常,需要轉向那個頁面,如果不定義path

預設情況下將使用<action>標籤中input屬性對應的頁面

* scope:可以取值requestsession,預設為request

* handler:異常的處理類,struts預設採用org.apache.struts.action.ExceptionHandler

如果做個性化的異常處理可以繼承此類覆寫相應的方法