<html:errors>标签与<html:messages>标签都是用来输出request或session范围内的消息的。它们之间有着相同的地方,也有不同的地方。
<html:errors/>标签
它表示输出request或session范围内的以“org.apache.struts.Globals.ERROR_KEY”为关键字存储的ActionMessages对象中的所有消息。
可通过以下属性的设置来输出指定的消息。
name属性:指定request或session范围内的关键字,该关键字指定了一个ActionMessages对象。若忽略该属性,则默认的关键字为“org.apache.struts.Globals. ERROR_KEY”。
property 属性:用于指定由name属性指定的关键字指定的ActionMessages对象中的某个消息属性,该消息属性指定了某个ActionMessage对象。若忽略该属性,则<html:errors/>标签会输出指定ActionMessages中的所有消息。
bundle属性:指定消息资源文件。若忽略该属性则从默认的消息资源文件ApplicationResources.properties中获取输出的消息内容。
<html:messages>标签
<html:messages>有一个必添项id
该标签与<html:errors>标签相似,同样用来输出消息。它们之间有着相同的地方,也有不同的地方。
1.与<html:errors>标签的相同点
消息来源:<html:messages>标签要输出的消息来源与<html:errors>标签相同,同样可以通过 saveErrors()或saveMessages()方法将ActionMessages对象存入request或session范围内。例如:
ActionMessage messages=new ActionMessages();
errors.add("username",new ActionMessage("com.logon.name.error"));
saveErrors(request,messages);
属性:<html:messages>标签的name属性、property属性、bundle属性以及footer属性、header属性分别与<html:errors/>标签中对应的属性用法相同。
2.与<html:errors>标签的不同点
属性
message 属性,<html:errors>标签中不存在的属性。当忽略该属性或将其设为“false”时,则根据由name属性指定的关键字在 request或session范围中查找该关键字存储的ActionMessages对象,若此时没有设置name属性,则查找以默认关键字 “org.apache.struts.Globals.ERROR_KEY”存储的ActionMessages对象;若将message属性设为 “true”,表示以“org.apache.struts.Globals.MESSAGE_KEY”关键字进行查找,并且此时设置的name属性无效。
id 属性,该属性是<html:messages>标签必须存在的属性,而<html:errors>标签中不存在该属性。它用来指定一个JSP页面变量,该变量代表了每次从ActionMessages对象中遍历出的一个ActionMessage对象。
使用方法:单独使用<html:messages>标签是不能输出消息的。因为该标签完成的操作只是对ActionMessages对象进行遍历,并将每次遍历出的ActionMessage对象存储在id属性所指定的JSP页面变量中。所以通常情况下嵌套<bean:write> 标签一起使用,<bean:write>标签中的name属性要与<html:messages>标签的id属性相匹配。
说明:<bean:write>标签用来输出由name属性指定的对象值。详细的介绍请参看本书中的第8章。
使用<html:messages>标签输出消息的代码如下。
<html:messages id="messageid" message="true">
<bean:write name="messageid"/>
</html:messages>
saveErrors(request,errors)方法将ActionMessages对象保存到了request范围内,并以“org.apache.struts.Globals.ERROR_KEY”关键字存储。Action基类中saveErrors()方法的关键代码如下。
protected void saveErrors(HttpServletRequest request, ActionMessages errors){
request.setAttribute(org.apache.struts.Globals.ERROR_KEY,errors);
}
在页面方可通过<html:errors bundle="user" property="nameEmpty"/>来显示相关错误的消息
或者
<html:messages id="error" bundle="user" property="nameEmpty" >
<bean:write name="error"/>
</html:messages>
来取property="nameEmpty"的单个的错误消息(注:id只是一个标示,只要和bean:write的name属性保持一致即可)
----------------------------------------
或者
<html:messages id="error" bundle="user">
<bean:write name="error" />
</html:messages>
来取所有的错误消息
相当于
<html:errors bundle="user"/>
另外,还可以通过saveMessages(request,errors)方法将ActionMessages对象保存到request范围内,但此时在request范围中以“org.apache.struts.Globals.MESSAGE_KEY”关键字存储。Action基类中saveMessages()方法的关键代码如下。
protected void saveMessages(HttpServletRequest request, ActionMessages errors) {
request.setAttribute(org.apache.struts.Globals.MESSAGE_KEY,errors);
}
所以如果使用了saveMessages()方法存储消息,在使用<html:errors>标签输出消息时,要通过name属性来指定在 request或session范围内以“org.apache.struts. Globals.MESSAGE_KEY”关键字来查找ActionMessages对象。否则此时忽略name属性,Struts会使用默认的关键字“org.apache.struts.Globals. ERROR_KEY”进行查找,将找不到存储的消息。
<html:messages id="error" bundle="user" message="true">
<bean:write name="error" />
</html:messages>
或者
<html:messages id="error" bundle="user" message="true" property="nameEmpty">
<bean:write name="error" />
</html:messages>
分别来取全部或者是单个的错误消息
注意点:
虽然Struts已经声明:不推荐使用ActionErrors & ActionError对象,但在一些遗留的系统中,依然还是可以看到其影子,所以如果你的系统不幸属于这样的两种混合系统,有以下的几种方法可以参考
1。两次调用messagesPresent,如下
<!-- Print ActionErrors Object -->
<logic:messagesPresent>
<html:messages id="msg" message="true">
<div class="success">
<bean:write name="msg"/>
</div><br/>
</html:messages>
</logic:messagesPresent>
<!-- Print ActionMessages Object -->
<logic:messagesPresent message="true">
<html:messages id="msg" message="true">
<div class="success">
<bean:write name="msg"/>
</div><br/>
</html:messages>
</logic:messagesPresent>
2.分别使用<html:messages> <html:errors>标签,当然在老系统中需要调用Action的saveErrors方法,而在新的应用中要调用saveMessages方法。
3.更换所有的ActionErrors为ActionMessages,并将所有调用saveErrors的地方更换成saveMessages,并将<html:errors>标签相应的更换成<html:messages message="true"> - 推荐!
相关推荐
------------异常处理程序<BR><BR>12.5一个异常处理的简单实例:除数为0<BR><BR>需求:使用一个小应用程序,完成两数相除的计算<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>分析:<BR>我们使用有顶向下,逐步求精的...
1. <s:generator separator="" val=""></s:generator>----和<s:iterator>标签一起使用 H: 1. <s:head/>-----在<head></head>里使用,表示头文件结束 2. <s:hidden></s:hidden>-----隐藏值 I: 1. <s:...
<FONT color="red" size="3"><html:errors/></FONT> <table> <html:form action="/login.do" method="post"> <tr> <td> 用户名 </td> <td> ...
</logic:notPresent> <html:errors property="username" /></td> </tr> <tr> <td><bean:message key="login.page.password" /></td> <td><logic:present name="loginFormBean"> ...
errors/><br/><html:form action="/regUserAction" focus="logname"><br/><table border="0" width="100%"><br/> <tr><br/> <th align="right"><br/> Logname:<br/> </th><br/> <td align="left"><br/> ...
Input/Output Using Stream Classes<br/><br/>13.1 Common Background of I/O Streams<br/><br/>13.2 Fundamental Stream Classes and Objects<br/><br/>13.3 Standard Stream Operators << and >><br/><br/>13.4 ...
<ignoreErrors>true</ignoreErrors> <url><![CDATA[http://t{$serverpart}.tianditu.cn/DataServer?T=cia_w&X={$x}&Y={$y}&L={$z}]]></url> <serverParts>0 1 2 3 5 6 7</serverParts> <backgroundColor>#...
<option name="ERRORS_ATTRIBUTES"> <value> <option name="FOREGROUND" value="ff0000" /> <option name="EFFECT_COLOR" value="ff0000" /> <option name="ERROR_STRIPE_COLOR" value="ff0000" /> <option ...
<id name="Id"> <generator class="guid"/> </id> <property name="OrderNumber"/> <property name="OrderDate"/> <many-to-one name="Customer" /> <set name="OrderLines" cascade="all-delete-orphan...
<br>Installation and use is governed by the license statement for RADStudio 2007.<br><br>This Hotfix applies to:<br><br>Product: RADStudio 2007, C++Builder 2007, Delphi 2007<br>Version: 2007<br>...
其它的运行符可以作用于数字和日期,但不能作用于字符串,大部分的时候,使用gt等字母运算符代替>会有更好的效果,因为 FreeMarker会把>解释成FTL标签的结束字符,当然,也可以使用括号来避免这种情况,如:<#if (x>y)> ...
the Web, or a network<br/>•Maintain and optimize application performance<br/><br/>Your kit includes:<br/>•NEW—60-day evaluation version of Microsoft Visual Studio® .NET 2003 Professional Edition ...
Test and debug coding errors<br/>•Deploy applications to your server or hosting service <br/>•Maintain and optimize applications<br/><br/>Your kit includes:<br/>•NEW—60-day evaluation version of ...
Neverthless, this is a good chance to enhance your debug abilty to eliminate those errors.<br/><br/>This book is compiled by matlab report generator and completely for free, you can share with your ...
maven的settings.xml配置,提供maven包下载位置,... <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror>
<h2>Login</h2> {{template "errors.html" .}} <form action="/login" method="post"> {{.CsrfTokenField}} <fieldset> <label for="email" class="label-required">Email</label> <input type=...
安装Gradlecompile ' io.github.cdimascio:japi-errors:1.3.1 ' 玛文< dependency> < groupId>io.github.cdimascio</ groupId> < artifactId>japi-errors</ artifactId> < version>1.3.1</ version></ dependency>...
鬼帮手托克目录的Ghost Blog把手助手正在安装将toc.js文件放入当前...用法{{toc}}{{toc start="1" end="3"}}结果: <li><a>Headline H1</a></li><ul> <li><a>Headline H2</a></li> <ul> <li><a>Headline H3</a></li>
dubbo找不到dubbo.xsd报错, cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'. ...of the document is not <xsd:schema>.