java - 尽管@NotNull和@Size注释,Spring mvc表单验证没有报告错误

标签 java spring validation spring-mvc

我正在学习java和spring。我尝试从《Spring in action 第四版》一书中测试表单验证。 无论我在表单字段中输入什么内容, validator 总是可以接受的。但是它应该拒绝空文本或太短的文本。 validator 是从 Hibernate 网页下载的。
我尝试了版本 4.2 和最新的稳定版本 6.1。
Spring 版本为 4.3.18。
我使用的是IntelliJ IDEA Ultimate 2019.3版本
作为服务器,我使用 Tomcat 9.0.27。
这里我放了一些来源:

Controller :

package org.maciek.second;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.validation.Valid;

@Controller
@RequestMapping(value = "/test")
public class TestController {
    @RequestMapping(method = RequestMethod.GET)
    public String page(Model model) {
        model.addAttribute("test",new Test());
        return "test/main";
    }

    @RequestMapping(value = "/process", method = RequestMethod.POST)
    public String page2(@Valid Test test, Errors errors, Model model) {
        if(errors.hasErrors()) {
            return "test/main";
        }
        System.out.println(test.getVal1());
        return "test/ok";
    }
}

测试类,其中 val1 从 Web 表单输入:

package org.maciek.second;

import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.Size;

public class Test {
    @NotEmpty
    @Size(min=3, message = "message")
    private String val1;

    public String getVal1() {
        return val1;
    }

    public void setVal1(String val1) {
        this.val1 = val1;
    }
}

Web .jsp 页面,表单简单:

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf" %>

<html>
<head>
    <title>Test</title>
</head>
<body>
<sf:form action="/test/process" commandName="test" method="post">
    <sf:input path="val1"/><sf:errors path="val1"/>
    <input type="submit" value="OK"/>
</sf:form>
</body>
</html>

WebAppInitializer

package org.maciek.second;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] {RootConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] {WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }

}

WebConfig

package org.maciek.second;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("org.maciek.second")
public class WebConfig extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

根配置

package org.maciek.second;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan(basePackages = {"org.maciek.second"}, excludeFilters = {
        @Filter(type= FilterType.ANNOTATION, value = EnableWebMvc.class)
})
public class RootConfig {

}

最佳答案

我可以肯定地说问题已经解决了。问题在于 IntelliJ IDEA 中未更新服务器工件。因此,所需的库未部署到服务器。正如我所说,我在学习,所以我会犯错误。我会记住这个教训。

关于java - 尽管@NotNull和@Size注释,Spring mvc表单验证没有报告错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59192640/

相关文章:

java - 使用 JTextField 创建/编辑对象

java - 找到两个整数数组的最长交集

asp.net-mvc-4 - 更改 asp.net mvc 4 模型中验证触发的顺序

jsf - 自定义验证器,我应该使用 FacesContext#addMessage() 还是抛出 ValidatorException?

java - Struts 2迭代器标签在jsp中不起作用?

Java Hibernate 删除对象

java - 使用mockito注入(inject)带有真实参数的bean

java - @ControllerAdvice不处理异常

spring - 当您 POST 到接受 HTTP GET 的 Controller 操作时,为什么 Spring MVC 不会抛出错误?

python - WTForms 验证器。可选 : continue validation of empty fields?