Java绿地(~ming~)

Java 草地

常用链接

统计

最新评论

struts中AtionErrors和ActionMessages的区别

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

web.xml:
  <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标签显示每个信息.例如:
<html:messages message="true" id="msg">
    <c:out value="${msg}"/><br />
</html:messages>

3. 具体的一个例子:
接受输入页面input.jsp:

  <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:
  <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:

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:

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");
}

 

posted on 2007-07-13 19:40 mrklmxy 阅读(1185) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: