http://xiangai.taobao.com
http://shop148612228.taobao.com

ActionError,ActionMessage推荐

尽管Struts框架供给了管用的失常处理机制,但不能保证处理所有的讹谬,这时Struts框架会把讹谬抛给Web容器,在默认情形下Web容器会向用户博览器直接归来原始消息。万一想避免直接让用户看到这些原始消息,能够在web.xml中搭配<error-page>元素,以下代码演示了如何避免用户看到HTTP 404、HTTP 500讹谬和Exception失常。
web.xml
Java代码

<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中无法对formbean数据举行检讨,因为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或JSTLc:out标签揭示每个消息.例如:
Java代码

<html:messagesmessage="true"id="msg"><c:outvalue="${msg}"/><br/></html:messages>

3cf.cfeits.com. 翔实的一个例子:
接受输入版面input.jsp:
Java代码

<html:formaction="/errormessage/input">phoneNumber:<html:textproperty="phoneNumber"/><html:errorsproperty="<%=org.apache.struts.action.ActionMessages.GLOBAL_MESSAGE%>"/><br/><html:submit/><html:cancel/></html:form>

struts-config.xml:
Java代码

<form-beans><form-beanname="inputForm"type="cn.rolia.struts.form.errorexception.InputForm"/></form-beans><action-mappings><actionattribute="inputForm"input="/errormessage/input.jsp"name="inputForm"path="/errormessage/input"scope="request"type="com.yourcompany.struts.action.errormessage.InputAction"validate="false"><forwardname="success"path="/errormessage/success.jsp"/></action></action-mappings>

InputAction.java:
Java代码

publicActionForwardexecute(ActionMappingmappingwww.ria38.com,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse){cn.rolia.struts.form.errorexception.InputForminputForm=(cn.rolia.struts.form.errorexception.InputForm)form;//TODOAuto-generatedmethodstubStringphoneNumber=inputForm.getPhoneNumber();if(phoneNumber.length()<4){ActionErrorsmessages=newActionErrors();messages.add(org.apache.struts.action.ActionMessages.GLOBAL_MESSAGE,newActionMessage("error.errormessage.input"));this.saveErrors(request,messages);returnmapping.getInputForward();}returnmapping.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, ActionMessagesmessages)措施和saveErrors(javax.servlet.http.HttpSession session,ActionMessages errors)措施。
InputAction.java:
Java代码

publicActionForwardexecute(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse){cn.rolia.struts.form.errorexception.InputForminputForm=(cn.rolia.struts.form.errorexception.InputForm)form;//TODOAuto-generatedmethodstubStringphoneNumber=inputForm.getPhoneNumber();if(phoneNumber.length()<4){ActionErrorsmessages=newActionErrors();messages.add(org.apache.struts.action.ActionMessages.GLOBAL_MESSAGE,newActionMessage("error.errormessage.input"));this.saveErrors(request.getSession(true),messages);returnmapping.getInputForward();}returnmapping.findForward("success");}

String temp = "Text here"; String s = new String (temp);

posted @ 2013-11-10 21:43  万事俱备就差个程序员  阅读(377)  评论(0编辑  收藏  举报

http://xiangai.taobao.com
http://shop148612228.taobao.com
如果您觉得对您有帮助.领个红包吧.谢谢.
支付宝红包
微信打赏 支付宝打赏