1. 程式人生 > >Struts2 (3) 異常處理

Struts2 (3) 異常處理

  1. 說到在Struts框架中的異常處理,很多人想到的應該是在Action中使用try  catch語句,然後再在配置檔案中進行進一步的處理
  2. 但是!這也 太low了吧,Struts其實有提供了更加酷和使用的異常處理方法,宣告式異常捕捉,這種方式是在Action的帶有 throws Exception的方法中丟擲異常,然後再struts.xml檔案中宣告異常的處理機制,具體如下
    1. 第一種是區域性宣告式異常處理,這是Action的程式碼
      package action;
      
      
      import java.sql.SQLException;
      
      import static com.opensymphony.xwork2.Action.ERROR;
      import static com.opensymphony.xwork2.Action.SUCCESS;
      
      /**
       * Demo class
       *
       * @author lin
       * @date 2018/11/9
       */
      public class TestAction {
          private String name;
      
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
          public String Test(){
              String standardName = "a";
              String name = getName();
              System.out.println(name);
              if( standardName.equals(name)){
      //            手動丟擲一個異常
                  return SUCCESS;
              }else {
                  return ERROR;
              }
          }
      
      
          public String execute() throws Exception{
              String sqlException = "sql";
              String standardName = "a";
      //        String name = getName();
              System.out.println(getName());
              System.out.println("kkk");
              if(sqlException.equals(getName())){
                  throw  new SQLException("sql");
              } else  if ( standardName.equals(name)){
                  return SUCCESS;
              }
              return ERROR;
          }
      }
      

      然後在struts.xml中宣告異常的處理,這裡是返回一個jsp頁面

      <?xml version="1.0" encoding="UTF-8"?>
      
      <!DOCTYPE struts PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
              "http://struts.apache.org/dtds/struts-2.5.dtd">
      
      <struts>
          <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
      
          <package name="test" extends="struts-default">
      
      
      
              <!--<global-results>-->
                  <!--<result name="my">index.jsp</result>-->
              <!--</global-results>-->
      
              <!--<global-exception-mappings>-->
                  <!--<exception-mapping exception="java.sql.SQLException" result="my"/>-->
              <!--</global-exception-mappings>-->
      
              <action name="*_Support" class="action.TestAction"  method="execute">
                  <exception-mapping exception="java.sql.SQLException" result="my"/>
                  <result name="my">exception.jsp</result>
                  <result name="success">ok!.jsp</result>
                  <result name="error">error.jsp</result>
                  <allowed-methods>Test</allowed-methods>
              </action>
      
          </package>
      </struts>

       

    2. 第二種是全域性變數宣告,其他的都一樣,struts.xml改成下面這樣就可以了

      <?xml version="1.0" encoding="UTF-8"?>
      
      <!DOCTYPE struts PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
              "http://struts.apache.org/dtds/struts-2.5.dtd">
      
      <struts>
          <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
      
          <package name="test" extends="struts-default">
      
      
      
              <global-results>
                  <result name="my">index.jsp</result>
              </global-results>
      
              <global-exception-mappings>
                  <exception-mapping exception="java.sql.SQLException" result="my"/>
              </global-exception-mappings>
      
              <action name="*_Support" class="action.TestAction"  method="execute">
                
                  <result name="success">ok!.jsp</result>
                  <result name="error">error.jsp</result>
                  <allowed-methods>Test</allowed-methods>
              </action>
      
          </package>
      </struts>

       

    3. 還可以在異常處理給到的那個jsp頁面中檢視異常的資訊

          <s:property value="exception.message"/>
      <%--檢視異常物件本身--%>
          <s:property value="exceptionStack"/>
      <%--檢視異常堆疊資訊--%>