![]() |
|
||||||||||||||
| | 网站首页 | 数据库教程 | web编程 | 服务器 | 程序设计 | | ||
|
||
|
||||||
| AtionErrors和ActionMessages的区别 | ||||||
作者:佚名 文章来源:不详 点击数: 更新时间:2007-9-2 ![]() |
||||||
|
尽管Struts框架提供了有效的异常处理机制,但不能保证处理所有的错误,这时Struts框架会把错误抛给Web容器,在默认情况下Web容器会向用户浏览器直接返回原始信息。
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()方法。 本文来源:http://blog.csdn.net/clickinghere/archive/2007/07/13/1688654.aspx
|
||||||
| 文章录入:admin 责任编辑:admin | ||||||
| 【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 | ||||||
| 网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!) |
| | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告 | 网站地图 | 管理登录 | | |||
|