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;

 

public class 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() {

       return errorCode;

    }

 

    public Object[] getParams() {

       return params;

    }

   

}

 

 

异常处理类

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;

 

public class SystemExceptionHandler extends ExceptionHandler {

 

    public ActionForward execute(

            Exception ex,

            ExceptionConfig ae,

            ActionMapping mapping,

            ActionForm formInstance,

            HttpServletRequest request,

            HttpServletResponse response)

            throws ServletException {

 

           if(!(ex instanceof SystemException)){

              return super.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

                          如果做个性化的异常处理可以继承此类覆写相应的方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值