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);
ActionError,ActionMessage推荐的更多相关文章
- Struts2的ActionError&ActionMessage示例
本教程显示使用Struts2的 ActionError 和 ActionMessage 类. 1. ActionError – 是用来发送错误信息反馈给用户 - 通过 <s:actionerro ...
- struts 标签库注解
在struts2中有着一套像html一样的标签,俗称struts2标签,大多数公司使用ssh都是使用html标签,但为了保持项目的统一性,有的公司还是使用的struts2的标签,下面是一些常用的str ...
- atitit.Atitit. Gui控件and面板-----服务端控件 java struts的实现最佳实践
atitit.Atitit. Gui控件and面板-----服务端控件 java struts的实现最佳实践 1. 服务器控件的类别 1 1.1. 数据控件:该类控件可细分为两种类型:数据源控件和数 ...
- ActionErrors和ActionError
**ActionErrors和ActionError都是ActionMessage的子类,ActionError存放在 ActionErrors中,ActionError对象中的参数为配置文件中配置的 ...
- 推荐一个ASP.NET网站内容管理系统源码
许多人都有各自的兴趣,如打球.踢毽子.看书.看电视.玩游戏等等....我近来迷上了猜灯谜,于是业余做了一个在线猜灯谜的网站:何问起谜语. 先出个谜语让你猜猜:不可缺一点(打一字).可以在线猜:http ...
- 推荐10款超级有趣的HTML5小游戏
HTML5的发展速度比任何人的都想像都要更快.更加强大有效的和专业的解决方案已经被开发......甚至在游戏世界中!这里跟大家分享有10款超级趣味的HTML5游戏,希望大家能够喜欢! Kern Typ ...
- 算是休息了这么长时间吧!准备学习下python文本处理了,哪位大大有好书推荐的说下!
算是休息了这么长时间吧!准备学习下python文本处理了,哪位大大有好书推荐的说下!
- 分享阿里云推荐码 IC1L2A,购买服务器可以直接打9折,另附阿里云服务器部署ASP.NET MVC5关键教程
阿里云推荐码为:IC1L2A 阿里云还是不错滴. 以windows server 2008 R2为例,介绍如何从全新的服务器部署MVC5 站点. 新购买的阿里云服务器是没有IIS的,要安装IIS: 控 ...
- Mac 词典工具推荐:Youdao Alfred Workflow(可同步单词本)
想必大家都有用过 Mac 下常见的几款词典工具: 特性 系统 Dictionary 欧路词典 Mac 版 有道词典 Mac 版 在线搜索 ✗ ✔ ✔ 屏幕取词 ☆☆☆ ★★☆ ★☆☆ 划词搜索 ★★★ ...
随机推荐
- NOIP 2013 day1
tags: 模拟 快速幂 逆序对 树状数组 归并排序 最小生成树 lca 倍增 categories: 信息学竞赛 总结 tex live 2017.iso 转圈游戏 火柴排队 货车运输 转圈游戏 s ...
- Combination Sum I&&II(经典的回溯算法题)
I: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C w ...
- 讲IOC非常好的一篇文章--初步弄懂DI
http://jinnianshilongnian.iteye.com/blog/1413846 http://jinnianshilongnian.iteye.com/blog/pdf 之后又看了类 ...
- [前端随笔][JavaScript][自制数据可视化] “中国地图”
说在前面 想自己实现一个可视化的中国地图(可以实现如用户来源省份数据统计功能),网上搜了一下,翻了几页几乎都是第三方库(如echarts.js)实现的,简直不能忍. 不是第三方库不好,只是要花时间去适 ...
- (翻译)Xamarin.Essentials: 移动应用的跨平台 API
原文地址:https://blog.xamarin.com/xamarin-essentials-cross-platform-apis-mobile-apps/ 当使用 Xamarin 开发 IOS ...
- cv论文(Low-rank相关)
最近把以前的几篇关于Low-rank的文章重新看了一遍,由于之前的一些积累,重新看一遍感觉收获颇多.写这篇博文的时候也参考了一些网上的博客,其中数这篇博文最为经典http://blog.csdn.ne ...
- 【PHPExcel实例】 php 导出 excel 实例
CREATE TABLE `person` ( `) DEFAULT NULL, `name` ) DEFAULT NULL, `birthday` date DEFAULT NULL ) ENGIN ...
- vmware漏洞之二——简评:实战VMware虚拟机逃逸漏洞
下文取自360,是vmware exploit作者自己撰写的.本文从实验角度对作者的文章进行解释,有助于学习和理解.文章虚线内或红色括号内为本人撰写. ------------------------ ...
- JavaScript Output
JS can "display" data in different ways: (1)Writing into an alert box, using window.alert( ...
- android:sharedUserId
<manifest> syntax: <manifest xmlns:android="http://schemas.android.com/apk/res/android ...