• struts声明式异常


    一,局部exception

    把action中产生的异常配置到struts-config.xml中,哪个action出现异常就在哪个action的配置中进行配置。

    <action path="/login"
        type="com.bjsxt.struts.LoginAction"
        name="loginForm"
        scope="request"
        validate="false"
        input="/login.jsp"
      > 
      <!--
       <exception key="user.not.found" type="com.bjsxt.struts.UserNotFoundException" path="/login_error.jsp"/>
       <exception key="user.password.error" type="com.bjsxt.struts.PasswordErrorException" path="/login_error.jsp"/>
       --> 
       <forward name="success" path="/login_success.jsp"/>
       <forward name="error" path="/login.jsp"/>
      </action>

    exception中key表示的是国际化中对应的信息

    type表示的是异常类

    handler表示自定义的异常处理方式

    如果要让出现异常后自动跳转到某页面,则在Action中配置属性input,或者在exception中添加path属性,其优先级高于Action中的input。

    二,全局exception

    <global-exceptions>
     <!--
       <exception key="user.not.found" type="com.bjsxt.struts.UserNotFoundException" path="/login_error.jsp"/>
       <exception key="user.password.error" type="com.bjsxt.struts.PasswordErrorException" path="/login_error.jsp"/>
      -->
     
      <!--
      <exception key="user.not.found" type="com.bjsxt.struts.UserNotFoundException" handler="org.apache.struts.action.ExceptionHandler"/>
      <exception key="user.password.error" type="com.bjsxt.struts.PasswordErrorException" handler="org.apache.struts.action.ExceptionHandler"/>
      -->
     
      <!--
      <exception key="error.exception" type="com.bjsxt.struts.ErrorCodeException" handler="com.bjsxt.struts.ErrorCodeExceptionHandler"/>
       -->
       <!--
       <exception key="error.exception" type="com.bjsxt.struts.AppException" handler="com.bjsxt.struts.AppExceptionHandler"/>
        --> 
      
       <exception key="error.exception" type="com.bjsxt.struts.AppException"/>
     </global-exceptions>

    三,struts实现声明式异常的方式

    如果Action中出现异常,会被Action中的异常捕获并向上抛出异常,而该异常会被struts中,调用processException()方法处理异常,使用反射查找配置的异常,如果没有配置那么就判断是否是IOException异常或者是ServletException异常,如果不是抛到web容器中,如果在web.xml中配置了该异常的处理方式,那么就处理如果没有配置那么就直接显示到页面;如果在struts中配置了,那么就按照配置的处理,struts创建handler类来处理配置的异常。

  • 相关阅读:
    [Python学习]Iterator 和 Generator的学习心得
    ubantu linux的bash shell初接触
    Linux-Ubuntu 启用root账户
    Ubuntu Linux系统三种方法添加本地软件库
    ASK,OOK,FSK的联系和区别
    spinlock一边连逻辑一边连控制器
    Cgroup与LXC简介
    关于 package.json 和 package-lock.json 文件说明
    ng build --aot 与 ng build --prod
    【Rxjs】
  • 原文地址:https://www.cnblogs.com/jinzhengquan/p/1955124.html
Copyright © 2020-2023  润新知