近日在做struts2框架验证中碰到了问题,弄了很久也没解决,希望哪位“大牛”可以帮帮忙@Validations(requiredStrings = {
@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "userDTO.userName", message = "登录名不能为空"),
@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "userDTO.password", message = "密码不能为空") })
public String login()@Validations(requiredStrings = {
@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "userDTO.registUserName", message = "注册名不能为空"),
@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "userDTO.registPassword", message = "注册密码不能为空") })
  public String regist()这是我在一个action中写的两个方法,分别对login和regist进行验证,一开始我以为在哪个方法上面写的就是对哪个方法进行验证,但是运行起来发现自己错了,会导致login时调用regist的方法,如果用@SkipValidation的话,就不会对表单提交的数据进行验证,但是实际情况是login需要执行login的验证,regist需要执行regist的验证,两个表单在一个页面中,后来在网上查到可以对action中的方法配置拦截器,就将login的代码改成如下:@Validations(requiredStrings = {
@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "userDTO.userName", message = "登录名不能为空"),
@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "userDTO.password", message = "密码不能为空") })
@Action(value="User_login",interceptorRefs = @InterceptorRef(value = "defaultStack", params = {
"validation.validateAnnotatedMethodOnly", "true",
"validation.excludeMethods", "regist" }))
public String login()这里User_login是actionName,对应的method是login,对这个方法进行配置拦截器让他可以在对login进行验证的时候不对regist方法做验证,不过在运行的时候,没有效果。不知道哪里出问题了?
我也试过@Action(value="User_login",interceptorRefs = @InterceptorRef(value = "validation", params = {
"validateAnnotatedMethodOnly", "true",
"excludeMethods", "regist" }))这么写,可是也没作用。
使用xml,用ActionClass-ActionAlias-validation.xml可以解决问题,不过我现在希望用annotation解决,既然annotation要实现的是零配置,我相信用annotation也可以解决,不过不知道错在哪里,希望哪位仁兄可以帮下忙,谢谢了!!