spring MVC中基于hibernate validator的form表单验证
来源:互联网 发布:毕向东25天java视频 编辑:程序博客网 时间:2024/05/19 20:00
首先,在pom.xml中添加hibernate.validator的支持
<!-- hibernate.validator --><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>4.3.1.Final</version></dependency>
表单提交页面user_add.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%><%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!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=UTF-8"><title>add user</title></head><body><form:form id="form" method="post"modelAttribute="validUserBean">添加用户<form:label path="account">账号:</form:label><form:input path="account" /><form:errors path="account" /><form:label path="password">密码:</form:label><form:input path="password" /><form:errors path="password" /><button type="submit">添加</button><button type="reset">重置</button><c:out value="${show}"></c:out></form:form></body></html>
新建一个用于验证的Bean:ValidUserBean
package com.worldless.admin.controller;import org.hibernate.validator.constraints.Email;import org.hibernate.validator.constraints.Length;import org.hibernate.validator.constraints.NotEmpty;import org.springframework.stereotype.Component;@Component("ValidUserBean")public class ValidUserBean {@NotEmpty@Emailprivate String account;@NotEmpty@Length(min=6,max=16)private String password;public String getAccount() {return account;}public void setAccount(String account) {this.account = account;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
来修改controller,
package com.worldless.admin.controller;import javax.validation.Valid;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.validation.BindingResult;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controllerpublic class AdminController {@Autowiredprivate ValidUserBean validUserBean; //通过url访问新增页面@RequestMapping(value = "admin/add", method = RequestMethod.GET)public String userNew(Model model) {model.addAttribute("validUserBean", validUserBean);return "admin/user_add";} //点击按钮提交时@RequestMapping(value = "admin/add", method = RequestMethod.POST)public String userAdd(@ModelAttribute("validUserBean") @Valid ValidUserBean validUserBean,BindingResult result, Model model) {if (!result.hasErrors()) {model.addAttribute("show", "ok");}return "admin/user_add";}}
访问页面
输入错误时显示
输入正确时显示
- spring MVC中基于hibernate validator的form表单验证
- spring MVC中基于hibernate validator的form表单验证
- spring MVC中基于hibernate validator的form表单验证
- Spring MVC基于hibernate validator的数据验证示例
- springMVC基于hibernate validator的表单验证示例
- jQuery Form Validator表单验证
- 怎么用spring mvc处理form表单的ajax验证
- spring-mvc的数据验证Validator
- spring mvc 结合Hibernate Validator进行服务器验证
- 表单的验证 Validator
- spring mvc 中使用spring的validator
- spring mvc 中使用spring的validator
- spring mvc 中使用spring的validator
- spring mvc 中使用spring的validator
- 基于Spring MVC框架JSR-303的自定义注解Validator验证实现
- 基于Spring MVC框架JSR-303的自定义注解Validator验证实现
- Spring MVC 整合 hibernate-validator
- 基于jquery的form表单验证
- [关于OPC的研究1]c# opc client源码调试和学习笔记
- IOS CoreLocation
- 开始学习c#的一些常用东西(希望也能帮到您啊)
- RMAN duplicate 异机恢复
- GPRS 模块调试总结
- spring MVC中基于hibernate validator的form表单验证
- dedecms后台模块管理空白解决方案。
- 把android 系统设为可写
- onNewIntent与singleTask
- 基于模型的整车电子电气架构设计
- CMFCToolBar::SetToolBarBtnText
- Linux系统中sit0是做什么的
- Android 亲测源码分享
- java socket聊天