`
okcomputer2009
  • 浏览: 35130 次
  • 性别: Icon_minigender_1
  • 来自: singapore
社区版块
存档分类
最新评论

Struts1.3 Tag总结之----Messages vs. Errors

阅读更多

信息标签 <html:messages />

Displays a set of messages prepared by a business logic component and stored as an ActionMessages object, ActionErrors object, a String, or a String array in any scope. If such a bean is not found, nothing will be rendered. The messages are placed into the page scope in the body of this tag where they can be displayed by standard JSP methods. (eg: <bean:write>,<c:out>,<%=%>)

 

以上是struts API doc<html:messages/>标签的表述。这里有几点需要注意:

首先,这些可供显示的对象,必须是由business logic component准备的。意思就是说是由我们程序添加进去的,而不是说会自动添加对象并寻找项目资源文件下的所有信息。

比如在Action类中:

request.setAttribute(Globals.ERROR_KEY, msgObj);

我们将对象保存进request scope.

 

<html:messages id="message" message="true" property="property2">

再次,该标签的属性 message

表示从所有scope中寻找名为什么的对象。true表示寻找名为 Globals.MESSAGE_KEY的对象,而不设该属性(默认情况下),它将寻找名为Globals.ERROR_KEY的对象。

Id属性声明了一个page scope可用的对象,来表示信息集合中的message

property属性则表示只取到ActionMessages集合中名为”property2”的对象,如果不设置,表示取到所有对象。然后可以通过<bean:write>,<c:out>,<%=%>等形式对id的声明代表的对象进行输出。

 

<html:messages id="message" message="true">

            <bean:write name="message"/>

</html:messages>

也就是说我们如果在Action类中 不将这个ActionMessages对象,以 Globals.ERROR_KEYGlobals.MESSAGE_KEY所代表的string存进某一个scope<html:messages/>标签将不能获取到。

 

 

Action类中:

ActionMessages msgs = new ActionMessages();

msgs.add("property1",

new ActionMessage("userRegistration.password.nomatch"));

      

msgs.add("property2",

new ActionMessage("img.text","1","1","1","1"));

 

request.setAttribute(Globals.MESSAGE_KEY, msgs);

相当于 saveMessages(request, msgs);

或者

request.setAttribute(Globals.ERROR_KEY, msgs);

相当于this.saveErrors(request, msgs);

      

ActionMessages/ActionErrors 是一个可以存放一系列信息(ActionMessage类)对象的集合。它的 add(String arg0, ActionMessage arg1)方法,arg0arg1: ActionMessage对象在该集合中的名字。

 

ActionMessage类可以用来保存MessageResources(项目信息资源文件)的内容。它的构造器 ActionMessage(String arg)中,arg是项目信息资源文件中的key名。它的第二个构造器 ActionMessage(String arg,String arg1)中,arg1是可以传递给项目信息资源文件的参数(占位符 place holder),这样的参数值多可以pass 四个。

 

jsp页面从ActionMessages/ActionErrors 集合类中取得ActionMessage对象时,就可以从项目信息资源文件中获取到和key相对应的value,并加以显示。

 

 

<html:errors bundle="extended"/>

<html:errors/> <html:messages/> 这两个标签作用都是从request scope里得到对象,只不过一个得到的是名为 Globals.ERROR_KEY 的对象,另一个是得到名为Globals.MESSAGE _KEY的对象(messages标签也可以取error对象)

默认情况会将所有对象输出,当指定了property属性的时候,就只输出指定对象。

ActionMessages msgs = new ActionMessages();也可以写做

ActionErrors msgs = new ActionErrors()

 

saveMessages(request, msgs); 就是将对象存为名为Globals.MESSAGE_KEY

saveErrors(request, msgs); 就是将对象存为名为Globals.ERROR_KEY

 

这样 jsp页面就可以用errors或者messages标签进行显示。 Messages标签即可显示Globals.MESSAGE_KEY (通过指定message=”true”) 又可显示Globals.ERROR_KEY (default)。而errors标签只能显示用saveErrors方法保存的信息对象。其实error还是message都是一个意思,建议用message标签。

 

这两个标签,最终的目的,是把(错误)信息从资源文件里读出来。Msgs是一个集合对象,它里面包含的每个元素,呈 name – ojb 形式,而该obj又是一个ActionMessage或者ActionError类的对象,用于初始该对象的值又对应了资源文件里的key。所以messageserorrs 标签的最终目的是通过keyresource bundle里的value读出来并加以显示。

分享到:
评论

相关推荐

    英文单词拼写混淆集:spell-errors.txt

    英文单词拼写混淆集:spell-errors.txt

    Struts2-ActionError-ActionMessage-Example

    Struts2是一个强大的MVC(模型-视图-控制器)框架,用于构建企业级Java Web应用程序。在Struts2框架中,ActionError和ActionMessage是处理用户输入验证和错误反馈的重要组件,它们使得开发者能够优雅地管理应用程序...

    print-winsock-errors.c

    libevent是2.1.8版本里面的test文件夹缺少print-winsock-errors.c,使得编译会出错

    官网下载:Windows-KB841290-x86-ENU.zip

    Windows-KB841290-x86-ENU.exe MD5值:58dc4df814685a165f58037499c89e76 ---------------------------------------------------...Fciv 2.05: Added success message if the verification did not detect any errors.

    opengoo 1.3 RC1

    Since 1.3-beta -------------- - usability: Emails with no subject can't be opened in Overview-&gt;View as list - usability: Added link to RSS feed for a workspace's recent activity in the dashboard ...

    cfg_errors.gocfg_errors.gocfg_errors.go

    cfg_errors.gocfg_errors.gocfg_errors.gocfg_errors.gocfg_errors.gocfg_errors.gocfg_errors.gocfg_errors.gocfg_errors.gocfg_errors.go

    java面试知识点总结--Struts.pdf

    Struts框架是Java Web开发中常用的MVC框架之一,它为构建基于J2EE的Web应用程序提供了结构化的解决方案。在面试中,理解Struts的工作原理和核心组件是至关重要的。 1. **Struts框架初始化流程**: 当Web应用启动时...

    Service Support-英文原版

    1.3 Target audience..............................................................................................................- 6 - 1.4 Navigating the IT Infrastructure Library........................

    T-REC-G.709.1-202005-I!Cor1!PDF-E.pdf

    - **Correction of Errors**: Corrigendum 1 likely corrects errors or discrepancies found in the original 2018 recommendation. - **Updated Guidelines**: It may include updated guidelines or additional ...

    struts2-tags-API.rar

    7. **Validation标签**:Struts2提供了一套强大的验证机制,`&lt;s:fielderror&gt;`和`&lt;s:errors&gt;`标签用于显示验证错误信息。 8. **Internationalization标签**:`&lt;s:text&gt;`和`&lt;s:message&gt;`标签用于国际化,可以从资源...

    struts1.x使用common-validator验证框架

    6. **显示错误信息**:在JSP页面上,使用Struts标签库的`&lt;html:errors&gt;`标签来显示全局错误,以及`&lt;html:messages&gt;`标签来显示特定字段的错误。 除了基本的验证功能,Common-Validator还支持自定义验证器,通过实现...

    Python库 | drf-friendly-errors-0.6.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:drf-friendly-errors-0.6.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    spell-errors.txt

    拼写纠错配套资源:该文件记录了很多用户写错的单词和对应正确的单词,可以通过该文件确定每个正确的单词所对应的错误拼写方式,并计算出每个错误拼写方式出现的概率

    Python库 | tabula_py-0.7.0-py2.py3-none-any.whl

    **Python库tabula-py详解** `tabula-py`是一个强大的Python库,专为从PDF文档中提取表格数据而设计。它简化了这个过程,使得开发者能够方便地将PDF中的表格转换为易于处理的数据结构,如Pandas DataFrame。...

    Struts-Localization-Example.zip_localization

    在JSP页面中,我们可以使用JSTL的`fmt`库或者Struts的`&lt;html:text&gt;`、`&lt;html:errors&gt;`等标签来引用资源文件中的内容。 为了根据用户的浏览器设置自动选择正确的Resource Bundle,Struts会检查`Accept-Language`请求...

    Angular-ngx-errors.zip

    Angular-ngx-errors.zip,用于reactive forms.ngx-errors的声明性验证错误模块,Angularjs于2016年发布,是Angularjs的重写版。它专注于良好的移动开发、模块化和改进的依赖注入。angular的设计目的是全面解决开发人员...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    Version 1.7 ----------- - ADD: Delphi/CBuilder 10.2 Tokyo now supported. - ADD: Delphi/CBuilder 10.1 Berlin now supported. - ADD: Delphi/CBuilder 10 Seattle now supported. - ADD: Delphi/CBuilder XE8 ...

    warnings-treated-as-errors.md

    # all warnings being treated as errors ## 技巧 在ubuntu系统下编译一个程序包,有时会遇到这样的错误: $ make ... cc1: all warnings being treated as errors

    Addison.Wesley.-.Imperfect.C++.Practical.Solutions.for.Real-Life.Programming.chm

    Help your compiler detect more errors and work more effectively Understand the aspects of style that impact reliability Apply the Resource Acquisition Is Initialization mechanism to a wide variety ...

    C-99-common-errors.zip_Common Errors

    "C-99-common-errors.zip" 包含了一个名为 "C 99个常见错误.pdf" 的文件,该文件详细阐述了99个C语言初学者可能会遇到的问题。以下是对这些常见错误的概述: 1. **语法错误**:这是最常见的错误类型,包括拼写错误...

Global site tag (gtag.js) - Google Analytics