gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:23:05 27 4
gpt4 key购买 nike

我正在学习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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com