Struts1.3 之流程框架概述4

7. Exception Handling with Struts

An exception handler allows you to declaratively handle an exception in the struts-config.xml file by associating an exception to a user friendly message and a user friendly JSP page that will display if the exception occurs.

 

Struts-config.xml

<action path="/userRegistration"

type="strutsTutorial.UserRegistrationAction"

name="userRegistrationForm"

input="/userRegistration.jsp">

 

<exception type="java.sql.SQLException"

key="userRegistration.sql.exception"

path="/userRegistrationException.jsp" />

 

<forward name="success" path="/regSuccess.jsp" />

<forward name="failure" path="/regFailure.jsp" />

</action>

 

Notice the use of <html:errors> to display the error message associated with the exception in JSP display.

 

 

SUMMARY

总的来说,sturts的配置文件[struts-config.xml,位于WEB-INF目录下]很重要。

 

<action-mappings>

 

<action path="/userRegistration"

type="strutsTutorial.UserRegistrationAction"

          name="userRegistrationForm"

          input="/jsp/userRegistration.jsp"

          validate="true"

          cancellable="true"

          attribute="user">

l        Path: 用户requestservlet

l        Type: 该请求所映射的action处理类

l        Name: 如果是input view, request表单所映射的ActionForm

l        Validate:    若为true,是否调用ActionFormvalidate()方法,缺省为true

l        Cancelable: 表示request表单中的<html:cancel />能否被点击,缺省为false

l        Attribute:  maps the ActionForm into a scope (session by default) under "user", 也就是说attribute会把formbean储存在一个叫做"user"的对象里, JSP页面可通过<bean:write name="user" property="firstName" /> 显示该对象里的内容

    

     <exception  type="java.sql.SQLException"

key="userRegistration.sql.exception" path="/jsp/userRegistrationException.jsp" />

l        Type: 异常类型

l        Key:  异常信息的键,用来获得显示给用户的error message

l        Path: 重定向到的页面

     

<forward name="success" path="/jsp/regSuccess.jsp" />

l        Action类返回的ActionFoward对象以及对应的jsp文件

   

</action-mappings>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值