这个就是Struts的数据验证问题(当然,有些也可以通过赋初始值办法解决),数据验证有两种方法,
在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;
}---------------------------------------------------------------------------
在struts中,js验证采用如下格式:
<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中,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;
}---------------------------------------------------------------------------
在struts中,js验证采用如下格式:
<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文件;
解决方案 »
- 真心求解:为啥会每次request后session都不一样
- 关于request传递参数的问题
- 关于读取表单中的信息(大家看看我的描述中能不能理解)
- struts中select问题
- 帮我看看是什么出错了吧,JSP 连ORACLE 谢谢谢谢!
- 小弟刚学extjs,这段代码看不懂,请各位高手帮忙解释一下,谢谢
- 用jspSmartUpload上传AutoCAD文件到数据库,下载下来出现乱码!!其他方式没乱码
- <c:forEach>嵌套循环问题
- 求助,关于JSP的Web应用的并发访问
- 十万火急!高分相送!JBULID配置
- 请问几个IREPORT问题 ,用熟悉IREPORT的高手请近来。急~~多谢各位指点!!
- 用ACCESS分页显示的问题.是不是ACCESS不支持rs.last()??
String a=(a!=null?a:"");
首先在ActionForm里我是这样定义的:
private Integer sign = null;
public Integer getSign()
{
return sign;
}
public void setSign(Integer aSign)
{
sign = aSign;
}在JSP页面上是这样的:
<html:text property="sign"/>这样页面第一页刷新时我在Action中输出:
System.out.println("form.getSign():" + form.getSign());
值为null;但我提交数据后,
System.out.println("form.getSign():" + form.getSign());
值成了0,实际上我在页面上什么也没有输,struts自动转成了0
为什么不是null?不解啊,我又没输0,非要使用String型吗?String型也是一个""空串,而不是null
具体参考文档