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"/>
      <%--查看异常堆栈信息--%>

猜你喜欢

转载自blog.csdn.net/weixin_39452731/article/details/83929327
今日推荐