簡體   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