ActionErrors 是继承ActionMessages的。以前都是ActionErrors,现在新版的struts多数用ActionMessages来代替。:)
解决方案 »
- 【满分】hadoop求解
- PDF报表,空白问题!
- 请教Java国际化问题
- response.sendRedirect在Struts中Action中应如何使用?给100分
- 关于request多页面传送的问题
- 如何分离用网页上传的数据流
- 把欠的80分补上, tomuno, jfy3d, supernicksun, leafxx,dongdong715进
- 简单的问题。各位请进!关于j2sdk和tomcat4.1。
- tomcat的小小问题,
- 哪位高手有时间,帮忙调一下程序,(送100分)
- 请高手推荐XML和UML的入门好书.
- |||||Connection reset by peer: socket write error怎么解决??参与有分||||||||
在第一个验证例子中,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>
2. 在struts-config.xml文件中添加ApplicationResources配置文件。如:
<message-resources parameter="com.dc.sibss.om.struts.ApplicationResources" />
添加此文件的作用是提供错误信息的非编程定制化和多语言支持。如果我们使用中文平台操作系统,则默认情况下将首先查找ApplicationResource_zh_CN.properties文件,然后是ApplicationResources_zh.properties,如果前两个文件没有被找到则将查找ApplicationResources.properties文件。
为了能够在页面上显示错误提示信息,我们还需要将以下内容添加到ApplicationResources.properties文件的末尾:
errors.required={0} is required.
errors.minlength={0} cannot be less than {1} characters.
errors.maxlength={0} cannot be greater than {2} characters.
errors.invalid={0} is invalid.
errors.byte={0} must be an byte.
errors.short={0} must be an short.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.float={0} must be an float.
errors.double={0} must be an double.
errors.date={0} is not a date.
errors.range={0} is not in the range {1} through {2}.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
以上仅是struts现在支持的错误类型的错误提示信息,如果你自定义了新类型的错误验证,则还需要在此加上你自己的内容。
以上内容中的{0}指的是错误提交的参数。比如:当你需要页面上的“用户名”不能为空时(也就是上面的errors.required),这个{0}就代表“用户名”,所以如果你没有填写用户名将抛出如下错误:
用户名 is required.(你可以根据需要修改称中文)
我们可能已经注意到了,既然错误提示信息需要配置,那么上例中“用户名”系统是如何得到的呢?没错!也是通过修改此配置文件,内容如下:
visitCust.error.name.required=<br/>用户名
这样当“用户名”为空时,struts后台程序将联合以上两处定义显示错误信息。
另外,上面的“visitCust.error.name.required”是在Validation.xml配置验证内容时指定的。具体见以下介绍。
注意:一般情况下,你的系统只需要一个ApplicationResources文件,所以开发组的成员不要添加自己的resource文件。只有在你的项目分组开发时才需要使用多个ApplicationResources文件,但是,同时你的struts-config.xml文件也会有相同的数量对应。
3. 创建你的ActionForm并让它继承org.apache.struts.validator.ValidatorForm类。
4. 创建你的Action实现,并和上面定义的ActionForm关联。这里需要注意的是,在定义此Action时一定将validate属性设置为true,并且在你定义的ActionForm中不要实现它的validate方法――这就意味着你将使用ValidatorForm的validate方法,这样才能保证你的错误验证正常进行。
5. 配置validation.xml文件。基本内容如下:
<form-validation>
<!-- ========== Default Language Form Definitions ===================== -->
<formset>
<form name="custGNewForm">需要验证页面上form的名字
<field property="certifiCode"需要校验的属性
depends="required,maxlength">校验内容
<arg0 key="prompt.certifiCode"/>ApplicationResource文件中对应
<arg1 key="${var:maxlength}" name="maxlength" resouce="false"/>
<var>确定最长限制的长度
<var-name>maxlength</var-name>
<var-value>20</var-value>
</var>
</field>
注意:此处的arg0和arg1就代表了ApplicationResources文件中使用“{}”括起来的参数。比如:
errors.range={0} is not in the range {1} through {2}.
定义了三个参数,所以你这里也要定义<arg0>、<arg1>、<arg2>三个参数才能完整的显示错误信息。
errors.maxlength={0} cannot be greater than {2} characters.
定义了0、2两个参数,所以你就需要定义<arg0>和<arg2>两个参数。
<field property="userName"
depends="required,maxlength">
<arg0 key="prompt.userName"/>
<arg2 key="${var:maxlength}" name="maxlength" resouce="false"/>
<var>
<var-name>maxlength</var-name>
<var-value>80</var-value>
</var>
</field>
<field property="email"
depends="email">
<arg0 key="prompt.email"/>
</field>
</form>
<form name="custGNewCheckForm">
<field property="certifiCode"
depends="required">
<arg0 key="prompt.certifiCode"/>
</field>
</form>
</formset>
</form-validation>
6. 在校验页面的<body>前添加如下内容:
<html:errors/>KUI回答:
在struts中,ActionForm验证方法采用如下格式:
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if((userName == null) || (userName.length() < 1)) {
errors.add("userName",
new ActionError("error.userName.required"));
}
if((password == null) || (password.length() < 1)) {
errors.add("password",
new ActionError("error.password.required"));
} return 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>
2. 在struts-config.xml文件中添加ApplicationResources配置文件。如:
<message-resources parameter="com.dc.sibss.om.struts.ApplicationResources" />
添加此文件的作用是提供错误信息的非编程定制化和多语言支持。如果我们使用中文平台操作系统,则默认情况下将首先查找ApplicationResource_zh_CN.properties文件,然后是ApplicationResources_zh.properties,如果前两个文件没有被找到则将查找ApplicationResources.properties文件。
为了能够在页面上显示错误提示信息,我们还需要将以下内容添加到ApplicationResources.properties文件的末尾:
errors.required={0} is required.
errors.minlength={0} cannot be less than {1} characters.
errors.maxlength={0} cannot be greater than {2} characters.
errors.invalid={0} is invalid.
errors.byte={0} must be an byte.
errors.short={0} must be an short.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.float={0} must be an float.
errors.double={0} must be an double.
errors.date={0} is not a date.
errors.range={0} is not in the range {1} through {2}.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
以上仅是struts现在支持的错误类型的错误提示信息,如果你自定义了新类型的错误验证,则还需要在此加上你自己的内容。
以上内容中的{0}指的是错误提交的参数。比如:当你需要页面上的“用户名”不能为空时(也就是上面的errors.required),这个{0}就代表“用户名”,所以如果你没有填写用户名将抛出如下错误:
用户名 is required.(你可以根据需要修改称中文)
我们可能已经注意到了,既然错误提示信息需要配置,那么上例中“用户名”系统是如何得到的呢?没错!也是通过修改此配置文件,内容如下:
visitCust.error.name.required=<br/>用户名
这样当“用户名”为空时,struts后台程序将联合以上两处定义显示错误信息。
另外,上面的“visitCust.error.name.required”是在Validation.xml配置验证内容时指定的。具体见以下介绍。
注意:一般情况下,你的系统只需要一个ApplicationResources文件,所以开发组的成员不要添加自己的resource文件。只有在你的项目分组开发时才需要使用多个ApplicationResources文件,但是,同时你的struts-config.xml文件也会有相同的数量对应。
3. 创建你的ActionForm并让它继承org.apache.struts.validator.ValidatorForm类。
4. 创建你的Action实现,并和上面定义的ActionForm关联。这里需要注意的是,在定义此Action时一定将validate属性设置为true,并且在你定义的ActionForm中不要实现它的validate方法――这就意味着你将使用ValidatorForm的validate方法,这样才能保证你的错误验证正常进行。
5. 配置validation.xml文件。基本内容如下:
<form-validation>
<!-- ========== Default Language Form Definitions ===================== -->
<formset>
<form name="custGNewForm">需要验证页面上form的名字
<field property="certifiCode"需要校验的属性
depends="required,maxlength">校验内容
<arg0 key="prompt.certifiCode"/>ApplicationResource文件中对应
<arg1 key="${var:maxlength}" name="maxlength" resouce="false"/>
<var>确定最长限制的长度
<var-name>maxlength</var-name>
<var-value>20</var-value>
</var>
</field>
注意:此处的arg0和arg1就代表了ApplicationResources文件中使用“{}”括起来的参数。比如:
errors.range={0} is not in the range {1} through {2}.
定义了三个参数,所以你这里也要定义<arg0>、<arg1>、<arg2>三个参数才能完整的显示错误信息。
errors.maxlength={0} cannot be greater than {2} characters.
定义了0、2两个参数,所以你就需要定义<arg0>和<arg2>两个参数。
<field property="userName"
depends="required,maxlength">
<arg0 key="prompt.userName"/>
<arg2 key="${var:maxlength}" name="maxlength" resouce="false"/>
<var>
<var-name>maxlength</var-name>
<var-value>80</var-value>
</var>
</field>
<field property="email"
depends="email">
<arg0 key="prompt.email"/>
</field>
</form>
<form name="custGNewCheckForm">
<field property="certifiCode"
depends="required">
<arg0 key="prompt.certifiCode"/>
</field>
</form>
</formset>
</form-validation>
6. 在校验页面的<body>前添加如下内容:
<html:errors/>KUI回答:
在struts中,ActionForm验证方法采用如下格式:
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if((userName == null) || (userName.length() < 1)) {
errors.add("userName",
new ActionError("error.userName.required"));
}
if((password == null) || (password.length() < 1)) {
errors.add("password",
new ActionError("error.password.required"));
} return errors;
}
<html:form action="/UserLoginAction" focus="username" onsubmit="return validateLogonForm(this);">
...................
<html:javascript formName="logonForm" dynamicJavascript="true" staticJavascript="false" />
<script language=Javascript1.1 src="staticJavascript.jsp"></script>
.............
其中logonForm在validation.xml中定义:
<form name="logonForm"> <field property="username"
depends="required, minlength,maxlength">
<arg0 key="prompt.username"/>
<arg1 key="${var:minlength}" name="minlength"
resource="false"/>
<arg2 key="${var:maxlength}" name="maxlength"
resource="false"/>
<var>
<var-name>maxlength</var-name>
<var-value>16</var-value>
</var>
<var>
<var-name>minlength</var-name>
<var-value>3</var-value>
</var>
</field> <field property="password"
depends="required, minlength,maxlength"
bundle="alternate">
<arg0 key="prompt.password"/>
<arg1 key="${var:minlength}" name="minlength"
resource="false"/>
<arg2 key="${var:maxlength}" name="maxlength"
resource="false"/>
<var>
<var-name>maxlength</var-name>
<var-value>16</var-value>
</var>
<var>
<var-name>minlength</var-name>
<var-value>3</var-value>
</var>
</field> </form>
对应的js代码在validator-rules.xml中定义:
<validator name="required"
classname="org.apache.struts.validator.FieldChecks"
method="validateRequired"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
msg="errors.required"> <javascript><![CDATA[
function validateRequired(form) {
var isValid = true;
var focusField = null;
var i = 0;
var fields = new Array();
oRequired = new required();
for (x in oRequired) {
var field = form[oRequired[x][0]];
if (field.type == 'text' ||
field.type == 'textarea' ||
field.type == 'file' ||
field.type == 'select-one' ||
field.type == 'radio' ||
field.type == 'password') {
var value = '';
// get field's value
if (field.type == "select-one") {
var si = field.selectedIndex;
if (si >= 0) {
value = field.options[si].value;
}
} else {
value = field.value;
}
if (value == '') {
if (i == 0) {
focusField = field;
}
fields[i++] = oRequired[x][1];
isValid = false;
}
}
}
if (fields.length > 0) {
focusField.focus();
alert(fields.join('\n'));
}
return isValid;
}]]>
</javascript> </validator>mickey_uuu() 回答:再补充点:
1自定义的ActionForm不在继承ActionForm 改为ValidatorForm; 2修改struts_config.xml:添加资源文件 plugin
在<struts-config>和</struts-config>之间添加
<message-resources parameter="resources.application" />
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" property="pathnames" />
</plug-in>
3 将validator-rules.xml,validation.xml拷贝到web-inf下
4 在web-inf/classed/resources/下建立 application.properties文件;
现在新版的struts多数用ActionMessages来代替
ActionErrors,和ActionMessages,本质上没有什么区别,而且实现的功能也几乎一样!
在struts1.2以及以后的版本中,
已经没有ActionErrors了,都用ActionMessages.
我在页面是这样写的:
<tr align="center">
<logic:messagesPresent message="true">
<html:messages id="message" message="true">
<%=message%>
</html:messages>
</logic:messagesPresent>
<td width="22%" class="tab-bag">
.....................
................
在Action里面是这样写的:
} catch(EJBException ex) {
TextLogger.getLogger().warning(ex.getMessage());
messages.add("addDevDefualtEvent",new ActionMessage("com.action.errors"));
ex.printStackTrace();
}
if(!messages.isEmpty()){
this.saveMessages(request,messages);
}
return mapping.findForward("monitorEvent");其中"com.action.errors"是已定义好了,但是在出错时页面上只是显示NULL
请各位高手指导一下,谢谢
Struts 在1.2 及以后的版本中已经废除了 ActionErrors,
请使用 ActionMessages 和 ActionMessage