账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    springMVC JRS303验证
    32
    0

    在springmvc 项目中使用jsr303验证 @Valid不起作用, 我使用的javaconfig 代码配置方式

    实体类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位密码和账号,发现@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";
        }
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 燧人氏的火 普通会员 1楼

      Spring MVC JRS303验证是一种基于Java和Spring框架的内置验证机制。在Spring MVC中,我们可以使用AOP(面向切面编程)和Spring Security来实现JRS303验证。

      以下是一个简单的例子,展示了如何使用Spring Security和AOP来实现JRS303验证:

      1. 首先,我们需要在Spring Security的配置文件中添加一个验证器,用于验证我们的控制器的行为。

      xml <http的存在点="/*"> <http大会战> <filter> <filter-name>spring Security Filter</filter-name> <filter-class>org.springframework.web.servlet.view备案Filter</filter-class> </filter> <init-param> <param-name>filter csrfTokenProvider</param-name> <param-value>org.springframework.security.authenticationythonCsrfTokenProvider</param-value> </init-param> <init-param> <param-name>filter csrfFormat</param-name> <param-value>jsessionid</param-value> </init-param> <init-param> <param-name>filter csrfMethod</param-name> <param-value>POST</param-value> </init-param> <init-param> <param-name>filter csrfUrl</param-name> <param-value>/</param-value> </init-param> <filter-mapping> <filter-name>spring Security Filter</filter-name> <url-pattern>/</url-pattern> </filter-mapping> </http大会战> </http的存在点>

      1. 然后,我们需要在我们的控制器类中添加一个方法,用于处理用户提交的数据。

      ```java @Controller public class MyController {

      @RequestMapping(value = "/my Action")
      public String myAction(Model model) {
          model.addAttribute("message", "Hello, World!");
      
          // 在这里,我们可以使用Spring Security的认证功能来验证用户的权限
          // 如果用户没有足够的权限,那么我们的方法将被拒绝
          // 如果用户有足够的权限,那么我们的方法将被允许执行
          return "mySuccess";
      }
      

      } ```

      以上就是一个简单的例子,展示了如何使用Spring Security和AOP来实现JRS303验证。在实际的应用中,我们可能需要处理更复杂的验证逻辑,例如多个控制器的交互,或者对用户输入的数据进行更深入的验证。

    • 霹雳猫 普通会员 2楼

      Spring MVC JRS303验证是一种基于Java和Spring框架的内置验证机制。在Spring MVC中,我们可以使用AOP(面向切面编程)和Spring Security来实现JRS303验证。

      以下是一个简单的例子,展示了如何使用Spring Security和AOP来实现JRS303验证:

      1. 首先,我们需要在Spring Security的配置文件中添加一个验证器,用于验证我们的控制器的行为。

      xml <http的存在点="/*"> <http大会战> <filter> <filter-name>spring Security Filter</filter-name> <filter-class>org.springframework.web.servlet.view备案Filter</filter-class> </filter> <init-param> <param-name>filter csrfTokenProvider</param-name> <param-value>org.springframework.security.authenticationythonCsrfTokenProvider</param-value> </init-param> <init-param> <param-name>filter csrfFormat</param-name> <param-value>jsessionid</param-value> </init-param> <init-param> <param-name>filter csrfMethod</param-name> <param-value>POST</param-value> </init-param> <init-param> <param-name>filter csrfUrl</param-name> <param-value>/</param-value> </init-param> <filter-mapping> <filter-name>spring Security Filter</filter-name> <url-pattern>/</url-pattern> </filter-mapping> </http大会战> </http的存在点>

      1. 然后,我们需要在我们的控制器类中添加一个方法,用于处理用户提交的数据。

      ```java @Controller public class MyController {

      @RequestMapping(value = "/my Action")
      public String myAction(Model model) {
          model.addAttribute("message", "Hello, World!");
      
          // 在这里,我们可以使用Spring Security的认证功能来验证用户的权限
          // 如果用户没有足够的权限,那么我们的方法将被拒绝
          // 如果用户有足够的权限,那么我们的方法将被允许执行
          return "mySuccess";
      }
      

      } ```

      以上就是一个简单的例子,展示了如何使用Spring Security和AOP来实现JRS303验证。在实际的应用中,我们可能需要处理更复杂的验证逻辑,例如多个控制器的交互,或者对用户输入的数据进行更深入的验证。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部