【发布时间】:2020-03-30 05:41:12
【问题描述】:
我正在学习 java 和 spring。我尝试从“Spring in action 第 4 版”一书中测试表单验证。
无论我在表单字段中输入什么,验证器总是可以的。但是它应该拒绝空的或太短的文本。验证器是从 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>
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[] {"/"};
}
}
网络配置
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 {
}
【问题讨论】:
-
它看起来不像你使用 Spring Boot(你真的应该),而且我没有看到任何地方配置了验证器。
-
我还没有使用 Spring Boot。这是我春季教育的下一步。就我搜索互联网而言,应该以某种方式自动添加一些 udemy 课程验证器。在所有示例中,我都没有看到没有人手动配置验证器。
-
需要更多关注:场景太多,没有调用验证器,调用了验证器但没有显示错误等等......你能调试并澄清到底发生了什么吗?
-
Boot 不是“高级”Spring,而是简单 Spring。从它开始。
-
我是春天的新手。我不知道如何检查是否调用了验证器。我确定
errors.hasErrors()返回错误。在控制台上,我看到System.out.println(test.getVal1());显示正确的值。正确的意思是输入到表单域的那个,但不应该被验证。
标签: java spring validation spring-mvc