Spring 3 MVC ConstraintValidator 验证:未调用 isValid 方法

编程入门 行业动态 更新时间:2024-10-25 19:23:55
本文介绍了Spring 3 MVC ConstraintValidator 验证:未调用 isValid 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我是 Spring 3 MVC 的新手,并试图在 ConstrainValidation-annotations/" rel="nofollow">this 但验证部分不起作用,isValid 方法没有被调用.不确定我的配置中是否缺少某些内容,以下是我尝试过的:

I am new to Spring 3 MVC and was trying to implemnt ConstrainValidation following this but the validation part did not worked, the isValid method did not got invoked. Not sure if I am missing something in my configuration, below is what I tried:

Employee.java

public class Employee {

@Phone
private String phone;

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}
}

电话.java

@Documented
@Constraint(validatedBy = {PhoneValidator.class})
@Target( { ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Phone {

String message() default "{Phone}";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

}

PhoneValidator.java

public class PhoneValidator  implements ConstraintValidator<Phone, String>{

@Override
public void initialize(Phone phone) {
    // TODO Auto-generated method stub

}

@Override
public boolean isValid(String phoneField, ConstraintValidatorContext cxt) {
    if(phoneField == null) {
        return false;
    }
    return phoneField.matches("[0-9()-\\.]*");
}
}

控制器

@RequestMapping(value="done")
public String displaySuccess(@Valid Employee employee,BindingResult result,Model model){
    if(result.hasErrors()){
        System.out.println("Validation Failed!!!");
        return "display";
    }else{
        System.out.println("Validation Succeeded!!!");
        return "done";
    }

}

Context.xml

<beans xmlns="http://www.springframework/schema/beans"
xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
xmlns:p="http://www.springframework/schema/p"
xmlns:context="http://www.springframework/schema/context"
xsi:schemaLocation="http://www.springframework/schema/beans
    http://www.springframework/schema/beans/spring-beans-3.0.xsd
    http://www.springframework/schema/context
    http://www.springframework/schema/context/spring-context-3.0.xsd">

<context:annotation-config />
<context:component-scan
    base-package="com.xxx" />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/pages/" />
    <property name="suffix" value=".jsp" />
</bean>
</beans>

有人可以指出我遗漏了什么吗?Phone 验证不起作用,isValid 方法也没有被调用.

Can someone please point out what am I missing? The Phone validation is not working, the isValid method is also not getting invoked.

推荐答案

更改为 解决了我的问题.

Changing <context:annotation-config /> to <mvc:annotation-driven /> resoled my problem.

参考:

Spring3 MVC:验证不起作用

这篇关于Spring 3 MVC ConstraintValidator 验证:未调用 isValid 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 17:24:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1139248.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   MVC   Spring   isValid   ConstraintValidator

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!