尽管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推荐的更多相关文章

  1. Struts2的ActionError&ActionMessage示例

    本教程显示使用Struts2的 ActionError 和 ActionMessage 类. 1. ActionError – 是用来发送错误信息反馈给用户 - 通过 <s:actionerro ...

  2. struts 标签库注解

    在struts2中有着一套像html一样的标签,俗称struts2标签,大多数公司使用ssh都是使用html标签,但为了保持项目的统一性,有的公司还是使用的struts2的标签,下面是一些常用的str ...

  3. atitit.Atitit. Gui控件and面板-----服务端控件 java struts的实现最佳实践

    atitit.Atitit.  Gui控件and面板-----服务端控件 java struts的实现最佳实践 1. 服务器控件的类别 1 1.1. 数据控件:该类控件可细分为两种类型:数据源控件和数 ...

  4. ActionErrors和ActionError

    **ActionErrors和ActionError都是ActionMessage的子类,ActionError存放在 ActionErrors中,ActionError对象中的参数为配置文件中配置的 ...

  5. 推荐一个ASP.NET网站内容管理系统源码

    许多人都有各自的兴趣,如打球.踢毽子.看书.看电视.玩游戏等等....我近来迷上了猜灯谜,于是业余做了一个在线猜灯谜的网站:何问起谜语. 先出个谜语让你猜猜:不可缺一点(打一字).可以在线猜:http ...

  6. 推荐10款超级有趣的HTML5小游戏

    HTML5的发展速度比任何人的都想像都要更快.更加强大有效的和专业的解决方案已经被开发......甚至在游戏世界中!这里跟大家分享有10款超级趣味的HTML5游戏,希望大家能够喜欢! Kern Typ ...

  7. 算是休息了这么长时间吧!准备学习下python文本处理了,哪位大大有好书推荐的说下!

    算是休息了这么长时间吧!准备学习下python文本处理了,哪位大大有好书推荐的说下!

  8. 分享阿里云推荐码 IC1L2A,购买服务器可以直接打9折,另附阿里云服务器部署ASP.NET MVC5关键教程

    阿里云推荐码为:IC1L2A 阿里云还是不错滴. 以windows server 2008 R2为例,介绍如何从全新的服务器部署MVC5 站点. 新购买的阿里云服务器是没有IIS的,要安装IIS: 控 ...

  9. Mac 词典工具推荐:Youdao Alfred Workflow(可同步单词本)

    想必大家都有用过 Mac 下常见的几款词典工具: 特性 系统 Dictionary 欧路词典 Mac 版 有道词典 Mac 版 在线搜索 ✗ ✔ ✔ 屏幕取词 ☆☆☆ ★★☆ ★☆☆ 划词搜索 ★★★ ...

随机推荐

  1. jmeter压力测试案例实战

    1.  测试目标地址:http://www.cnblogs.com/ 2.  1秒内有100个用户同时访问,看性能如何 3.  步骤 线程组.http请求.查看结果树.聚合报告 添加http请求如下: ...

  2. HIbernate学习笔记5 之 查询

    一.HQL查询 * 按条件查询,条件中写的是属性名,之后在query对象为添加赋值,如: String hql = " from User where uid=?"; Sessio ...

  3. Mybatis学习—XML映射文件

    总结自 Mybatis官方中文文档 Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同 ...

  4. 魔法上网之Ubuntu部署“酸酸”

    “酸酸”,即s*h*a*d*o*w*s*o*c*k*s,用于魔法上网,用python写成. 在ubuntu环境下,用pip包管理工具可以非常方便地安装“酸酸”服务:ssserver. 先安装pip(假 ...

  5. mybatis spring sqlsession

    sqlsession是什么? 从 http://blog.csdn.net/hupanfeng/article/details/9238127 知道 sqlsession创建 可以看出,创建sqlse ...

  6. Android点击图标重新启动问题

    原文:http://blog.csdn.net/jianiuqi/article/details/54091181 项目中的小问题:发现应用打包安装后按home键切换到后台后,点击应用图标又重新打开了 ...

  7. lr_Vugen界面图

  8. Team Service 编译项目并生成项目

    第一步:生成GitHub帐号连接 在Service中选择Github 在弹出的GitHub连接中点击授权,即会弹出另一个窗口,输入Github的用户名及口令,即可授权. 第二步:创建Build定义 解 ...

  9. centos7中安装wdcp管理系统(用于网站搭设)

    首先我们进入官网看下安装方法https://www.wdlinux.cn/wdcp/install.html 可以看到,实际上有两张安装方式,一种是源码进行安装,还有一种是RPM包安装,显然第二种安装 ...

  10. Codeforces Round #278 (Div. 1) Strip (线段树 二分 RMQ DP)

    Strip time limit per test 1 second memory limit per test 256 megabytes input standard input output s ...