在struts中使用saveErrors,保存的error,有时要用ActionErrors,有时又要用ActionMessages
我用的是jbuilderX,我不明白,有时要用ActionErrors,有时又要用ActionMessages
否则jbuilder就会保存
------解决方法--------------------------------------------------------
下面是前面我回答过的类似问题:
struts系列问题1: validator例子中为什么不用html:errors
在第一个验证例子中,registration.jsp有这样一段:
<logic:messagesPresent>
<bean:message key="errors.header"/>
<ul>
<html:messages id="error">
<li><bean:write name="error"/></li>
</html:messages>
</ul><hr>
</logic:messagesPresent>
根据文档logic:messagesPresent缺省在request中查询Globals.ERROR_KEY属性.
<html:errors>应该也可以达到同样效果,样式在四个errors.xxx中定义.
为什么不简化它呢?
另外,很多录入画面在提交有错误时,只出现有错误的栏位,在Struts中可以做到这种效果吗?
同时用<html:errors/>和<logic:messagesPresent> ,即如下:
<html:errors/>
<logic:messagesPresent>
<bean:message key="errors.header"/>
<ul>
<html:messages id="error">
<li><bean:write name="error"/></li>
</html:messages>
</ul><hr>
</logic:messagesPresent>
------------
运行结果是:
Validation Error
You must correct the following error(s) before proceeding: First Name can not be less than 5 characters. Last Name is required. Address is required. City is required. State is required. Zip is required. E-mail is required.
Validation Error
You must correct the following error(s) before proceeding:
First Name can not be less than 5 characters.
Last Name is required.
Address is required.
City is required.
State is required.
Zip is required.
E-mail is required.
----------------------------
可见,采用<html:errors/>时是把所有出错信息简单连在一起显示,而后者是一个信息一个信息地提出来显示,可自由定义每个出错信息的显示方式,上述方式是换行,当前也还可在中间加入别的信息。当然,如果只有一个出借信息,那么上述两者的显示结果是一样的。
**************
更重要的一点是,出错信息是采用了setAttribute()函数来保存数据的,所以可以采用各种带有getAttribute()函数来显示,可见出错信息用<logic:Iterate>来提取也是一样的。
可见,在Action、ActionForm、自定义标签文件或其它类文件中也可以直接用setAttribute()函数来保存出错数据。
Struts的Validation机制,请指教!
truehappy(天涯海角) 回答:
1.在struts-config.xml文件中加入validator插件,加入这个插件后你的应用就具别的使用validator的环境,但是,要想真正的使用它还需要其他的配置。如:
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" property="pathnames" />
</plug-in>