- 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 条
- 全部回答
-
燧人氏的火 普通会员 1楼
Spring MVC JRS303验证是一种基于Java和Spring框架的内置验证机制。在Spring MVC中,我们可以使用AOP(面向切面编程)和Spring Security来实现JRS303验证。
以下是一个简单的例子,展示了如何使用Spring Security和AOP来实现JRS303验证:
- 首先,我们需要在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的存在点>- 然后,我们需要在我们的控制器类中添加一个方法,用于处理用户提交的数据。
```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验证:
- 首先,我们需要在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的存在点>- 然后,我们需要在我们的控制器类中添加一个方法,用于处理用户提交的数据。
```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验证。在实际的应用中,我们可能需要处理更复杂的验证逻辑,例如多个控制器的交互,或者对用户输入的数据进行更深入的验证。
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部
