添加bean-validator.jar包
修改实体类User.java,添加默认构造器以及数据验证信息
package com.test.model; import javax.validation.constraints.Size; import org.hibernate.validator.constraints.Email; import org.hibernate.validator.constraints.NotEmpty; public class User { private String username; private String nickname; private String password; private String email; public User() { super(); } public User(String username, String nickname, String password, String email) { super(); this.username = username; this.nickname = nickname; this.password = password; this.email = email; } @NotEmpty(message="邮箱不能为空!") @Email(message="邮箱格式不正确!") public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } @NotEmpty(message="密码不能为空!") @Size(min = 6, max = 10, message = "密码的长度必须在5到10位!") public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @NotEmpty(message="用户名不能为空!") public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
在Action控制器UserController.java中添加方法:
/** * 添加用户前 * * @param model * @return */ @RequestMapping(value = "/addUserPro", method = RequestMethod.GET) public String addUserPro(Model model) { // 如果不添加下面这一句的话,sf:form标签中的modelAttribute="user"会在request中找user实例,没有的话会报错 model.addAttribute(new User()); return "user/addUser"; } /** * 添加用户 * * @param user * @return */ @RequestMapping(value = "/addUser", method = RequestMethod.POST) // 这里的参数user要和页面上modelAttribute属性值一致 public String addUser(@Valid User user, BindingResult br) {// 紧跟Valid参数之后写验证结果类 if (br.hasErrors()) { return "user/addUser"; } users.put(user.getUsername(), user); return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/user/userList"; }
修改用户列表页面/jsp/user/userList.jsp添加:<a href="addUserPro">添加用户</a>
添加用户新增页面/jsp/user/addUser.jsp
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>添加用户</title> </head> <body> <sf:form method="post" modelAttribute="user" action="addUser"> 用户名*:<sf:input path="username"/><sf:errors path="username" cssStyle="color:red"/><br> 别名:<sf:input path="nickname"/><br> 密码*:<sf:password path="password"/><sf:errors path="password" cssStyle="color:red"/><br> 邮箱*:<sf:input path="email"/><sf:errors path="email" cssStyle="color:red"/><br> <input type="submit" value="添加"/> </sf:form> </body> </html>
在springmvc配置文件spring-mvc.xml中添加<mvc:annotation-driven/>,如果没有这个标签,程序会直接跳过验证逻辑,因为它提供@Valid支持(很惭愧,研究了半天,只知其一,不知其二,知道这个标签里面注入了两个bean,但不知道在什么情况下用必须这个标签)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <mvc:annotation-driven/> <context:component-scan base-package="com.test.controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 全局异常处理 --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <!-- 设置显示异常信息的视图逻辑 --> <prop key="com.test.exception.UserException">user/loginUser</prop> </props> </property> </bean> </beans>
为了解决中文乱码问题,在web.xml中添加
<filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
在浏览器中输入:
http://127.0.0.1:9900/springmvc_005_rest_03/user/,进入到用户添加页面,不填写任何信息,直接点击“登录”按钮,页面显示
相关推荐
这是我的这篇博文的源码:SpringMVC与SiteMesh2.4无缝整合并借助JSR303规范实现表单验证,博文地址:http://blog.csdn.net/jadyer/article/details/7574668
项目中所需的jar包在压缩包中已提供,项目中用到了JSR303和hibernate-validator的技术,大量采用了注解@NotNull,@Email,@Length,@Max,@Pattern,@Size等,此外还采用了自定义注解,验证信息全部配置在属性文件中.
压缩包里面包含的是我所在springmvc项目里面的springmvc以及JSR303数据验证说依赖的jar包,可以方向使用,亲测有效
SpringMVC 数据的格式化、JSR 303数据校验和国际化 实例.zip 对应博客:http://blog.csdn.net/peng_hong_fu/article/details/53559773
springMVC spring mybatis rest风格架构 根据请求的后缀名 解析成json 或者 xml格式的数据
SpringMVC JSR303数据校验DEMO(附:JSR303的JAR包) SpringMVC JSR303数据校验DEMO(附:JSR303的JAR包)
NULL 博文链接:https://yingzhuo.iteye.com/blog/1455438
springmvc中rest风格的增删改查,适合初学者参考
hibernate-validator-5.2.4.Final.jar是对上述接口的实现;或者在Maven中添加依赖:1.<dependency> 2. <groupId>org.hibernate</groupId> 3. <artifactId>hibernate-validator</artifactId> 4. <version>5.2.4.Final...
SSM笔记-SpringMVC REST风格初识、RequestMapping/PathVariable/RequestParam/RequestHeader/CookieValue基本使用方法
hibernate-validator-5.2.4.Final.jar hibernate-validator-annotation-processor...validation-api-1.1.0.Final.jar jboss-logging-3.2.1.Final.jar classmate-1.1.0.jar slf4j-api-1.6.1.jar slf4j-log4j12-1.6.1.jar
企业级开发-SpringMVC使用拦截器实现用户登录权限验证实验报告
SpringMVC构建REST接口:第四篇 第一个REST风格的接口的源代码
Springboot SpringMVC thymeleaf页面提交Validation实现实例.pdfSpringboot SpringMVC thymeleaf页面提交Validation实现实例.pdfSpringboot SpringMVC thymeleaf页面提交Validation实现实例.pdfSpringboot SpringMVC...
SpringMVC构建REST接口的源代码
使用spring+springmvc框架 整合了Mybatis持久层框架 整合Druid用于数据库连接,并使用Druid对业务层监控spring jdbc 整合EhCache,对Mybatis的二级缓存进行管理和对spring进行缓存管理 整合FastJson对指定http类型的...
SpringMVC的服务器端的验证JSR303所需jar包
使用SpringMVC的验证器完成货物入库输入校验。验证入库时间,以及商品价格是否合理等。主要用到的JSR303中:@Past:验证Date和Calender对象是否在当前时间之前。@Pattern:验证String对象是否符合正则表达式的规则。
Springboot SpringMVC thymeleaf页面提交Validation实现实例.docxSpringboot SpringMVC thymeleaf页面提交Validation实现实例.docxSpringboot SpringMVC thymeleaf页面提交Validation实现实例.docxSpringboot ...
主要介绍了springmvc Rest风格介绍及实现代码示例,rest风格简洁,分享了HiddenHttpMethodFilter 的源码,通过Spring4.0实现rest风格源码及简单错误分析,具有一定参考价值,需要的朋友可以了解下。