ActionError,ActionMessage介绍

 

尽管Struts框架提供了有效的异常处理机制,但不能保证处理所有的错误,这时Struts框架会把错误抛给Web容器,在默认情况下Web容器会向用户浏览器直接返回原始信息。如果想避免直接让用户看到这些原始信息,可以在web.xml中配置<error-page>元素,以下代码演示了如何避免用户看到HTTP 404、HTTP 500错误和Exception异常。

web.xml
Java代码 复制代码 收藏代码
  1. <error-page> 
  2.     <error-code>404</error-code> 
  3.     <location>/exception/error404.jsp</location> 
  4.   </error-page> 
  5.   <error-page> 
  6.     <error-code>500</error-code> 
  7.     <location>/exception/error500.jsp</location> 
  8.   </error-page> 
  9.   <error-page> 
  10.     <exception-type>java.lang.Exception</exception-type> 
  11.     <location>/exception/default.jsp</location> 
  12.   </error-page> 
<error-page>
    <error-code>404</error-code>
    <location>/exception/error404.jsp</location>
  </error-page>
  <error-page>
    <error-code>500</error-code>
    <location>/exception/error500.jsp</location>
  </error-page>
  <error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/exception/default.jsp</location>
  </error-page>


当WEB容器捕获到exception-type或error-code指定的错误时将跳到由location指定的页面。

问题:当form bean 为动态bean时,在action中无法对form bean数据进行验证,因为formbean没有具体实现类。action中无法引用 ActionError/ActionErrors/ActionMessage/ActionMessages:
有时候你需要向用户提供相关处理信息,包括表单验证时发现错误等。
1. 相关类介绍:
ActionMessage:用于保存一个与资源束对应的提示信息。主要构造函数如:
ActionMessage(String message);
ActionMessage(String message,paramater)。

ActionMessages:用于保存多个ActionMessage。并在html:errors 和html:messages中起作用。
主要构造函数:
ActionMessages().
主要方法是add(String property,ActionMessage message)
ActionMessages 有一个HashMap类型messages保存多个ActionMessage对象,每个ActionMessage对象都有唯一的一个property 标识。这个property可以是自定义的任意字符串,也可以由 org.apache.struts.action.ActionMessages.GLOBAL_MESSAGE指定
html:messages/html:errors使用property属性访问某个资源

ActionErrors:用于保存一个与资源束对应的错误信息。用法跟ActionMessages差不多。
ActionError不赞成使用。


2. 版本:
struts1.1中用ActionErrors报告错误,用ActionMessages提供信息。
在struts1.2中使用ActionMessages提供信息和错误,不赞成使用ActionError
struts1.3中已经没有ActionError类了。

3. AtionErrors和ActionMessages的区别

1. ActionErrors是ActionMessages的一个子类,功能几乎相同,不同点在于标签<html:errors/>和<html:messages>的使用上的区别。
html: errors指定了footer和header属性。默认值为errors.header和errors.footer,需要时可以自己指定。如果资源属性文件配置了 errors.header和errors.footer,则任何时候使用html:errors时开头和结尾都是这两个属性对应的资源信息。
而html:message默认情况下没有errors.header和errors.footer值,当然可以自己指定。

2. html:errors可以根据property属性指定显示一个错误信息。html:messages有一个必添项id。html:messages不能直接显示信息,它将选出的信息放入一个用id标识的Iterator对象里,然后在用ben:write或JSTL c:out标签显示每个信息.例如:
Java代码 复制代码 收藏代码
  1. <html:messages message="true" id="msg"
  2.     <c:out value="${msg}"/><br /> 
  3. </html:messages> 
<html:messages message="true" id="msg">
    <c:out value="${msg}"/><br />
</html:messages>


3. 具体的一个例子:
接受输入页面input.jsp:
Java代码 复制代码 收藏代码
  1. <html:form action="/errormessage/input"
  2.     phoneNumber : <html:text property="phoneNumber"/> <html:errors     property="<%=org.apache.struts.action.ActionMessages.GLOBAL_MESSAGE %>"/><br/> 
  3.   <html:submit/><html:cancel/> 
  4.   </html:form> 
<html:form action="/errormessage/input">
    phoneNumber : <html:text property="phoneNumber"/> <html:errors     property="<%=org.apache.struts.action.ActionMessages.GLOBAL_MESSAGE %>"/><br/>
  <html:submit/><html:cancel/>
  </html:form>


struts-config.xml:
Java代码 复制代码 收藏代码
  1. <form-beans > 
  2.     <form-bean name="inputForm" type="cn.rolia.struts.form.errorexception.InputForm" /> 
  3.   </form-beans> 
  4.   <action-mappings > 
  5.     <action 
  6.       attribute="inputForm" 
  7.       input="/errormessage/input.jsp" 
  8.       name="inputForm" 
  9.       path="/errormessage/input" 
  10.       scope="request" 
  11.       type="com.yourcompany.struts.action.errormessage.InputAction" 
  12.       validate="false"
  13.       <forward name="success" path="/errormessage/success.jsp" /> 
  14.     </action> 
  15.   </action-mappings> 
<form-beans >
    <form-bean name="inputForm" type="cn.rolia.struts.form.errorexception.InputForm" />
  </form-beans>
  <action-mappings >
    <action
      attribute="inputForm"
      input="/errormessage/input.jsp"
      name="inputForm"
      path="/errormessage/input"
      scope="request"
      type="com.yourcompany.struts.action.errormessage.InputAction"
      validate="false">
      <forward name="success" path="/errormessage/success.jsp" />
    </action>
  </action-mappings>


InputAction.java:
Java代码 复制代码 收藏代码
  1. public ActionForward execute(ActionMapping mapping, ActionForm form, 
  2.     HttpServletRequest request, HttpServletResponse response) { 
  3.   cn.rolia.struts.form.errorexception.InputForm inputForm = (cn.rolia.struts.form.errorexception.InputForm) form;// TODO Auto-generated method stub 
  4.   String phoneNumber = inputForm.getPhoneNumber(); 
  5.   if(phoneNumber.length()<4){ 
  6.   ActionErrors messages = new ActionErrors(); 
  7.     messages.add(org.apache.struts.action.ActionMessages.GLOBAL_MESSAGE,new ActionMessage("error.errormessage.input")); 
  8.     this.saveErrors(request, messages); 
  9.     return mapping.getInputForward(); 
  10.   } 
  11.  
  12.   return mapping.findForward("success"); 
public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
  cn.rolia.struts.form.errorexception.InputForm inputForm = (cn.rolia.struts.form.errorexception.InputForm) form;// TODO Auto-generated method stub
  String phoneNumber = inputForm.getPhoneNumber();
  if(phoneNumber.length()<4){
  ActionErrors messages = new ActionErrors();
    messages.add(org.apache.struts.action.ActionMessages.GLOBAL_MESSAGE,new ActionMessage("error.errormessage.input"));
    this.saveErrors(request, messages);
    return mapping.getInputForward();
  }

  return mapping.findForward("success");
}


解说:用户输入手机号码,页面跳转到InputAction控制层进行处理,若输入数据小于4,则创建一个ActionMessage类存储相关错误信息。然后再创建ActionErrors类将此ActionMessage放入ActionErrors。再调用Action的saveErrors方法将此ActionErrors保存的request范围里,然后返回input.jsp页面要求重新输入并用html:errors提示错误信息。

4. Action包含saveErrors()方法和saveMessages()方法。如果创建的ActionErrors则应该调用saveErrors(),若创建的是ActionMessages则应该调用saveMessages()方法。
saveErrors ()接收ActionMessages而不是ActionErrors;同时将其保存在request中并用一个由 org.apache.struts.Globals.ERROR_KEY指定的常量” org.apache.struts.Globals.ERROR_KEY”标识这个ActionMessages,便于html:errors查找。 saveMessages()方法接收ActionMessages同时将其保存在request中并用一个由 org.apache.struts.Globals.MESSAGE_KEY指定的常量” org.apache.struts.Globals.MESSAGE_KEY”标识这个ActionMessages,进而让html: messages从常量Globals.ERROR_KEY中遍历获取信息。可以将其属性message设置为true,那么它将从常量 Globals.MESSAGE_KEY中遍历获取信息。

5. 默认情况下html:messages从
如果你想将信息保存在session里而不是request,struts1.2提供了
struts1.1 没有的saveMessages(HttpSession session, ActionMessages messages)方法和saveErrors(javax.servlet.http.HttpSession session, ActionMessages errors)方法。

InputAction.java:
Java代码 复制代码 收藏代码
  1. public ActionForward execute(ActionMapping mapping, ActionForm form, 
  2.     HttpServletRequest request, HttpServletResponse response) { 
  3. cn.rolia.struts.form.errorexception.InputForm inputForm = (cn.rolia.struts.form.errorexception.InputForm) form;// TODO Auto-generated method stub 
  4.   String phoneNumber = inputForm.getPhoneNumber(); 
  5.   if(phoneNumber.length()<4){ 
  6.     ActionErrors messages = new ActionErrors(); 
  7.     messages.add(org.apache.struts.action.ActionMessages.GLOBAL_MESSAGE,new ActionMessage("error.errormessage.input")); 
  8.     this.saveErrors(request.getSession(true), messages); 
  9.     return mapping.getInputForward(); 
  10.   } 
  11.  
  12.   return mapping.findForward("success"); 
public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
cn.rolia.struts.form.errorexception.InputForm inputForm = (cn.rolia.struts.form.errorexception.InputForm) form;// TODO Auto-generated method stub
  String phoneNumber = inputForm.getPhoneNumber();
  if(phoneNumber.length()<4){
    ActionErrors messages = new ActionErrors();
    messages.add(org.apache.struts.action.ActionMessages.GLOBAL_MESSAGE,new ActionMessage("error.errormessage.input"));
    this.saveErrors(request.getSession(true), messages);
    return mapping.getInputForward();
  }

  return mapping.findForward("success");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值