Struts异常处理机制

Struts异常处理

      1)、将Action的处理方法写成将异常抛出。throws Exception

      2)、在Struts.xml中配置异常处理。

  异常处理:

        局部异常:将<exception-mapping.../>元素作为<action.../>元素的子元素配置。

        全局异常:将<exception-mapping.../>元素作为<global-exception-mapping.../>的子元素配置,放在<package.../>标签下。

    

下面是一个异常处理的例子:

     程序请单:src\org\crazyit\app\action\LoginAction.java

      public class LoginAction extends ActionSupport{

               private String username;

               private String password;

               private String tip;

              //此处省略getter和setter方法

              public String execute() throws Exception{

                       if(getUsername().equalsIgnoreCase("user"){

                            throw new MyException("自定义异常“);

                       }

                      if(getUsernam().equalsIgnoreCase("sql"){

                             throw new java.sql.SQLException("用户名不能为sql“):

                     }

                      if(getUsernam().equalsIgnoreCase("admin"){

                             addActionMessage("哈哈,服务器提示”);

                             return SUCCESS;

                       }

                       return ERROR;

                  }

       }

      程序清单:

             src\struts.xml

           <?xml version="1.0" encoding="UTF-8"?>

           <!DOCTYPE struts PUBLIC 

                        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

                        "http://struts.apache.org/dtds/struts-2.3.dtd">

   

           <struts>

                <package name="lee" extends="struts-default">

                     <!-- 定义全局结果映射   -->

                     <global-results>

                          <result name="sql">/WEB-INF/content/exception.jsp</result>          

                          <result name="root">/WEB-INF/content/exception.jsp</result>    

                      </global-results>    

                     <!-- 定义全局异常 -->

                     <global-exception-mappings>

                              <!--  sql为转向的页面 -->

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

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

                      </global-exception-mappings>

                      <action name="login" class="org.crazyit.app.action.LoginAction">

                                <! -- 定义局部异常 -->

                                <exception-mapping exception="og.crazyit.app.exception.MyException" result="my"/>

                                <result name="my">/WEB-INF/content/exception.jsp</result>  

                                 <result name="success">/WEB-INF/content/welcome.jsp</result>  

                      </action>

                      <action name=*">

                                 <result>/WEB-INF/content/{1}.jsp</result>

                     </action>

               </struts>  

    获取异常信息:

           可以在exception页面通过以下方式获取异常信息:

            <s:property value=“exception.message"/>输出异常信息

            <s:property value="exceptionStack"/>输出堆栈信息

猜你喜欢

转载自collegeyuan.iteye.com/blog/2275790