在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ springMVC JRS303驗(yàn)證

springMVC JRS303驗(yàn)證

在springmvc 項目中使用jsr303驗(yàn)證 @Valid不起作用, 我使用的javaconfig 代碼配置方式

實(shí)體類User:

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配置:

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 配置:

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配置:

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 {
}

測試頁面:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/12/19 0019
  Time: 10:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="registerInfo" method="post">
    用戶名:<input name="username"/><br/>
    密碼:<input name="password"/>
    <br/>
    <button  type="submit">提交</button>
</form>
</body>
</html>
<script>


</script>

我在頁面輸入1位密碼和賬號,發(fā)現(xiàn)@Valid并沒有起作用,dubug也是error為0. 求解該怎么配置啊,怎么不起作用,搞了好久

    @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";
    }
回答
編輯回答
毀與悔

你是不是沒有引入這個校驗(yàn)的實(shí)現(xiàn)類?這個在spring mvc里只是一個接口具體實(shí)現(xiàn)還需要引入jar包,例如下面的:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
<version>4.2.0.CR1</version>
</dependency>
2017年7月12日 18:21