繁体   English   中英

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

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

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

控制器

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 的测试类:

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>

网络应用初始化器

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[] {"/"};
    }

}

网络配置

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 中未更新的服务器工件。 因此,所需的库没有部署到服务器。 正如我所说,我正在学习,所以我会犯错误。 我会记住这个教训。

暂无
暂无

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

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