springMVC 验证器
采用Hibernate-validator来进行验证,Hibernate-validator实现了JSR-303验证框架支持注解风格的验证。首先我们要到http://hibernate.org/validator/下载需要的jar包,这里以4.3.1.Final作为演示,解压后把hibernate-validator-4.3.1.Final.jar、jboss-logging-3.1.0.jar、validation-api-1.0.0.GA.jar这三个包添加到项目中。
/wzz/src/main/resources/applicationContext-mvc.xml
<?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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.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.controller" /> <!-- 对某些静态资源,如css,js,图片等进行过滤 ,有引用 "/resources/**" 的路径引用转到工程的/resources/目录取资源 -->
<mvc:resources mapping="/resources/**" location="/resources/" /> <!-- jsp 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean> <!-- 默认的注解映射的支持 -->
<mvc:annotation-driven validator="validator" conversion-service="conversion-service" /> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
<!--不设置则默认为classpath下的 ValidationMessages.properties -->
<property name="validationMessageSource" ref="validatemessageSource"/>
</bean>
<bean id="conversion-service" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
<bean id="validatemessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:validatemessages"/>
<property name="fileEncodings" value="utf-8"/>
<property name="cacheSeconds" value="120"/>
</bean>
</beans>
/wzz/src/main/java/com/controller/GeneralController.java
package com.controller; import javax.validation.Valid; 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; import com.java.LoginMessage; @Controller
public class GeneralController { @RequestMapping(value="/login_input", method = {RequestMethod.GET})
public String login_input(Model model){ if(!model.containsAttribute("loginMessage")){
model.addAttribute("loginMessage", new LoginMessage());
}
return "login";
} @RequestMapping(value="/login_input", method = {RequestMethod.POST})
public String login_save(@Valid @ModelAttribute("loginMessage") LoginMessage loginMessage, BindingResult bindingresult, Model model){
if(bindingresult.hasErrors()){
return login_input(model);
}
return "success";
}
}
/wzz/src/main/java/com/java/LoginMessage.java
package com.java; import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size; import org.hibernate.validator.constraints.NotEmpty; public class LoginMessage { @Pattern(regexp="^[a-zA-z0-9]+$", message="not has space.")
private String fullname; @Size(min=3, max=20, message="The password must be at least 6 charaters long.")
private String password; public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} }
/wzz/src/main/webapp/WEB-INF/views/login.jsp
<%@ 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"> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form:form modelAttribute="loginMessage" method="post">
<!-- path="*" 错误提示一起出现 -->
<%-- <form:errors path="*"></form:errors><br/><br/> --%> fullname:<form:input path="fullname" /><br/>
<form:errors path="fullname"></form:errors><br/> password:<form:input path="password" /><br/>
<form:errors path="password"></form:errors><br/> <input type="submit" value="Submit" /> </form:form>
</body>
</html>
springMVC 验证器的更多相关文章
- SpringMVC拦截器(实现登录验证拦截器)
本例实现登陆时的验证拦截,采用SpringMVC拦截器来实现 当用户点击到网站主页时要进行拦截,用户登录了才能进入网站主页,否则进入登陆页面 核心代码 首先是index.jsp,显示链接 <%@ ...
- SpringMVC拦截器+Spring自定义注解实现权限验证
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- SpringMVC表单验证器
本章讲解SpringMVC中怎么通过注解对表单参数进行验证. SpringBoot配置 使用springboot,spring-boot-starter-web会自动引入hiberante-valid ...
- springMVC中使用 JSR-303验证器( Validation 接口 )
在pom.xml,添加validator验证器的依赖 <dependency> <groupId>org.hibernate</groupId> <artif ...
- SpringMVC拦截器的使用
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- SpringMVC拦截器详解[附带源码分析]
目录 前言 重要接口及类介绍 源码分析 拦截器的配置 编写自定义的拦截器 总结 总结 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:h ...
- SpringMVC拦截器详解
拦截器是每个Web框架必备的功能,也是个老生常谈的主题了. 本文将分析SpringMVC的拦截器功能是如何设计的,让读者了解该功能设计的原理. 重要接口及类介绍 1. HandlerExecution ...
- CSS样式表、JS脚本加载顺序与SpringMVC在URL路径中传参数与SpringMVC 拦截器
CSS样式表和JS脚本加载顺序 Css样式表文件要在<head>中先加载,这样网页显示时可以第一次就渲染出正确的布局和样式,网页就不会闪烁,或跳变 JS脚本尽可能放在<body> ...
- spring mvc: Hibernate验证器(字段不能为空,在1-150自己)
spring mvc: Hibernate验证器(字段不能为空,在1-150自己) 准备: 下载Hibernate Validator库 - Hibernate Validator.解压缩hibern ...
随机推荐
- Margin and Padding in Windows Forms Controls
https://msdn.microsoft.com/en-us/library/ms229627.aspx Margin and Padding Precise placement of contr ...
- Bug避障算法简介
移动机器人智能的一个重要标志就是自主导航,而实现机器人自主导航有个基本要求--避障.避障是指移动机器人根据采集的障碍物的状态信息,在行走过程中通过传感器感知到妨碍其通行的静态和动态物体时,按照一定的方 ...
- Populating Display Item Value On Query In Oracle Forms
Write Post-Query trigger for the block you want to fetch the field value for display item.ExampleBeg ...
- sencha touch建立新项目
首先你得有一个sencha touch的环境,如下图: 图1 touch的sdk环境 有了这个之后,通过在cmd中执行下列命令: sencha -sdk /path/to/framework gene ...
- 05_IOC容器装配Bean(注解方式)
IOC容器装配Bean(注解方式) 1.使用注解方式进行Bean注册 xml 方式: <bean id="" class=""> spring2.5 ...
- CUBRID学习笔记 38 net调用java的函数过程
首先要设置java_stored_procedure 为yes 该配置项在cubrid.conf中 书写并编译java代码 public class SpCubrid{ public st ...
- 1.mybatis简介
mybatis简介 MyBatis 是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为My ...
- Yii2文件上传
首先在app\controllers下建立TestController.php,内容为如下代码: <?php namespace app\controllers; use Yii; use yi ...
- mysql概要(十五)存储过程
1.定义: 2.查看所有存储过程: show procedure status; 3.创建存储过程: create procedure 存储过程名字(参数) begin s1l语句; end$ ...
- css3动画导航实现
代码 <!DOCTYPE html> <!-- saved from url=(0223)file:///C:/Users/user/AppData/Local/Temp/HZ$D. ...