java - Hibernate 验证 没有验证

标签 java validation spring-mvc spring-3 hibernate-validator

嗨,我正在遵循我发现的示例

http://www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/

问题是我发布的个人资料中没有发现任何错误。我应该。为什么会发生这种事?

@Test
@Ignore
public void anotherTest() {
    Profile profile = ProfileUtil.getProfile();

    profile.setEmail("user@mail.com");
    profile.setSex("dafjsgkkdsfa");
    BindingResult bindingResult = new BeanPropertyBindingResult(profile, "profile");
    userController.postUser(new ModelMap(), profile, bindingResult);

    if (bindingResult.hasErrors()) {
        System.out.println("errors");
    }

    assertTrue(bindingResult.hasErrors());

    profileService.deleteProfile(profile);
}


@RequestMapping(value = "/", method = RequestMethod.POST)
public View postUser(ModelMap data, @Valid Profile profile, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            System.out.println("No errors");
            return dummyDataView;
        }

        data.put(DummyDataView.DATA_TO_SEND, "users/user-1.json");
        profileService.save(profile);
        return dummyDataView;
}

编辑: 这是个人资料。我现在正在测试性别,所以我想这才是重要的。

package no.tine.web.tinetips.domain;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import no.tine.web.tinetips.util.CommonRegularExpressions;

import org.hibernate.validator.constraints.NotBlank;


@Entity
public class Profile {

    @Id
    @GeneratedValue
    private Long id;

    @NotNull(message = "profile.email.null")
    @NotBlank(message = "profile.email.blank")
    @Size(max = 60, message = "profile.email.maxlength")
    @Pattern(regexp = CommonRegularExpressions.EMAIL, message = "profile.email.regex")
    @Column(name = "Email", unique = true)
    private String email;

    @Pattern(regexp = "^[M|F]{1}$", message = "profile.sex.regex")
@Size(max = 1, message = "profile.sex.maxlength")
private String sex;


}

最佳答案

基本上,您使用 this.userController = new UserController() 实例化了一个 POJO,然后调用其方法 this.controller.postUser(...)。只是带有简单对象的简单 Java,与 Spring 和 Spring MVC 没有任何关系:不考虑 @Valid。

如果你想让它工作,你必须为你的测试类提供一些Spring信息,使用@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(...)。然后,对于 Spring MVC 部分,您必须通过一些 Spring MVC 工具模拟 Controller 上的请求调用。如果您使用 Spring MVC 3.0- 或 3.1+,则执行方式会有所不同。如需更多信息和实际代码,see this post and its answers ,例如。

关于java - Hibernate 验证 没有验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12602731/

相关文章:

java - Swing 错误 ClassCastException

spring - @Autowired 是否只引用一个对象?

spring - @ExceptionHandler + @ResponseStatus

更改 dispatcherServlet url 映射后找不到 Spring Actuator 端点

Java 重绘方法不起作用,为什么?

java - Spring BeanPostProcessor 不会调用其实现的 bean ,而是调用所有其他 bean

java - 为什么我无法以编程方式关闭 JavaFX 上的对话框?

java - Spring 条件属性验证

php - 在 codeigniter 中编辑表单的独特验证

java - 当用户输入字母字符时,Play 对数字字段进行验证