作者:朵朵妞er | 来源:互联网 | 2024-12-20 09:07
在springmvc 项目中使用jsr303验证 @Valid不起作用, 我使用的javaconfig 代码配置方式实体类User:1234567891011121314151617181920212
在springmvc 项目中使用jsr303验证 @Valid不起作用, 我使用的javaconfig 代码配置方式
实体类User:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package spring.entity;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
public class User implements Serializable {
@NotNull
@Size(min = 5,max = 10,message = "用户名长度5到10")
private String username;
@NotNull
@Size(min = 5,max = 10,message = "密码长度5到10")
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
} |
mvc配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package spring.config;
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(basePackages = "spring")
public class MvcConfig extends WebMvcConfigurerAdapter{
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
viewResolver.setExposeContextBeansAsAttributes(true);
return viewResolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
} |
webxml 配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package spring.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MvcInitlizer extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
protected Class>[] getServletConfigClasses() {
return new Class[]{MvcConfig.class};
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
} |
spring配置:
1 2 3 4 5 6 7 8 9 10 11
| package spring.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan(basePackages = "spring.config")
public class SpringConfig {
} |
测试页面:
我在页面输入1位密码和账号,发现@Valid并没有起作用,dubug也是error为0. 求解该怎么配置啊,怎么不起作用,搞了好久
1 2 3 4 5 6 7 8
| @PostMapping(value = "/registerInfo")
public String hi(@Valid User user, Errors errors){
if(errors.hasErrors()){
return "hi";
}
System.out.println("mabi:"+user.getUsername());
return "redirect:/userbase";
} |