我在基于struts验证框架的下面写了一个自定义的验证规则,如下:
我的验证类:NewValidator2
public class NewValidator2 {
public static boolean validateFile(Object bean,
ValidatorAction va,Field field,
ActionMessages errors,
HttpServletRequest request){
UserForm userform=(UserForm)bean;
FormFile file=userform.getUpload();
String fullname=file.getFileName();
int i=fullname.lastIndexOf(".");
String extendname=fullname.substring(i);
if(!(extendname.equals(".jpg")||extendname.equals(".gif")))
{
errors.add(field.getKey(),Resources.getActionMessage(request, va, field));
}
return (errors.isEmpty());
}
public static boolean validateFileLength(Object bean,
ValidatorAction va,Field field,
ActionMessages errors,
Validator validator,
HttpServletRequest request){
UserForm userform=(UserForm)bean;
FormFile file=userform.getUpload();
if(file.getFileSize()>100000)
{
errors.add(field.getKey(), Resources.getActionMessage(validator,request, va, field));
}
return (errors.isEmpty());
}
}
我的validation.xml文件的相关部分:
<field property="upload" depends="myvalidate2length,myvalidate2">
<arg0 key="label.uploadFile"/>
<arg1 name="myvalidate2length" key="${var:maxlength}" resource="false"/>
<var>
<var-name>maxlength</var-name>
<var-value>100k</var-value>
</var>
</field>
我的validator.xml文件的相关部分:
<validator name="myvalidate2"
classname="com.zw.fantasticblog.util.NewValidator2"
method="validateFile"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.validateFile"/>
<validator name="myvalidate2length"
classname="com.zw.fantasticblog.util.NewValidator2"
method="validateFileLength"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.validateFileLength"/>
资源文件和页面肯定没有问题,我出现的问题就是:
1,在我的validation.xml文件的相关部分,depends指定了两个验证,但是总是第一个验证能够执行,第二个验证不能执行。
2,在我的验证类中,那个长度100000应该是从validation.xml中取出来的,但是总找不到方法。
希望大家帮忙看看,thanks!!
我的struts是1.2版本。
我的验证类:NewValidator2
public class NewValidator2 {
public static boolean validateFile(Object bean,
ValidatorAction va,Field field,
ActionMessages errors,
HttpServletRequest request){
UserForm userform=(UserForm)bean;
FormFile file=userform.getUpload();
String fullname=file.getFileName();
int i=fullname.lastIndexOf(".");
String extendname=fullname.substring(i);
if(!(extendname.equals(".jpg")||extendname.equals(".gif")))
{
errors.add(field.getKey(),Resources.getActionMessage(request, va, field));
}
return (errors.isEmpty());
}
public static boolean validateFileLength(Object bean,
ValidatorAction va,Field field,
ActionMessages errors,
Validator validator,
HttpServletRequest request){
UserForm userform=(UserForm)bean;
FormFile file=userform.getUpload();
if(file.getFileSize()>100000)
{
errors.add(field.getKey(), Resources.getActionMessage(validator,request, va, field));
}
return (errors.isEmpty());
}
}
我的validation.xml文件的相关部分:
<field property="upload" depends="myvalidate2length,myvalidate2">
<arg0 key="label.uploadFile"/>
<arg1 name="myvalidate2length" key="${var:maxlength}" resource="false"/>
<var>
<var-name>maxlength</var-name>
<var-value>100k</var-value>
</var>
</field>
我的validator.xml文件的相关部分:
<validator name="myvalidate2"
classname="com.zw.fantasticblog.util.NewValidator2"
method="validateFile"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.validateFile"/>
<validator name="myvalidate2length"
classname="com.zw.fantasticblog.util.NewValidator2"
method="validateFileLength"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.validateFileLength"/>
资源文件和页面肯定没有问题,我出现的问题就是:
1,在我的validation.xml文件的相关部分,depends指定了两个验证,但是总是第一个验证能够执行,第二个验证不能执行。
2,在我的验证类中,那个长度100000应该是从validation.xml中取出来的,但是总找不到方法。
希望大家帮忙看看,thanks!!
我的struts是1.2版本。
解决方案 »
- tomcat的服务器怎么都访问不上
- 求救:type Exception report
- 关于struts2中session的问题···不明白
- VerifyError错误
- jsp运用SmartUpload上传组件的问题,急啊,请高手帮帮忙!!!
- 如果实现外部提示表单到服务器端的数据库?
- lucene索引问题求教
- 请给一个在Servlet中用socket的例子,谢谢了
- 啊!!!!受不了了,怎么把image类型的对象转化成bufferedimage的对象呀?急用!!!快疯了
- tomcat如何配置添加目录?Tomcat Administration的用户/密码是什么?
- tomcat如何发布java工程
- spring和struts整合,不理解!
减少重复开发~`避免错误
<controller>元素:用于配置ActionServlet.buffreSize指定上载文件的输入缓冲大小.该属性为可选默认4096.className指定元素对应的配置类,ControllerConfig.然后是contentType指定响应结果内容类型和字符编码,该属性为可选,默认是text/html如果在Action或者JSP网页也设置了类型内容,会覆盖这个.locale指定是否把Locale对象保存到当前用户的session中默认false.tempDir指定处理文件上载的临时工作目录.nochache如果是true在响应结果中加入特定的头参数.
http://forum.csdn.net/PointForum/SearchTopic.aspx?bigclassid=00000000-0000-0000-0000-000000000000&smallclassid=df8a1eb4-7ecb-4fcd-a9ae-1be6fd084aeb&searchtype=big&subject=&username=albertadream&pb=pb