简体   繁体   English

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

[英]Spring mvc form validation reports no errors despite @NotNull and @Size annotations

I'm learning java and spring.我正在学习java和spring。 I tried to test form validation from book "Spring in action 4th edition".我试图从“Spring in action 4th edition”一书中测试表单验证。 No matter what I input into form field it's always OK for the validator.无论我在表单字段中输入什么,验证器总是可以的。 However it should reject empty or too short text.但是它应该拒绝空的或太短的文本。 Validator is downloaded from Hibernate webpage. Validator 是从 Hibernate 网页下载的。
I tried version 4.2 and latest stable 6.1.我尝试了 4.2 版和最新的稳定版 6.1。
Spring is in version 4.3.18. Spring 版本为 4.3.18。
I use IntelliJ IDEA Ultimate in version 2019.3我在 2019.3 版本中使用 IntelliJ IDEA Ultimate
As server I use Tomcat 9.0.27.作为服务器,我使用 Tomcat 9.0.27。
Here I put some source:我在这里放了一些来源:

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";
    }
}

Test class with val1 entered from web form:从网络表单输入 val1 的测试类:

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 with simple form:具有简单形式的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();
    }
}

RootConfig根配置

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 {

}

I can definitelly say that problem is solved.我可以肯定地说这个问题已经解决了。 Problem was with not updated server artifacts in IntelliJ IDEA.问题在于 IntelliJ IDEA 中未更新的服务器工件。 Because of this required libraries were not deployed to server.因此,所需的库没有部署到服务器。 As I said I'm learning so I make mistakes.正如我所说,我正在学习,所以我会犯错误。 I'll remember this lesson.我会记住这个教训。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM