springmvc服务端数据验证
网上有很多好的文章,本人主要是参照了开涛的博客做的验证,这里谈一下个人的具体使用,和一些在他的博客中没讲到的具体问题的处理。
用的是基于jsr-303验证框架做的声明式服务端数据验证。至于编程式,我就不讲了,我也没用到,不过声明式有时候不能完全满足要求,在这时可能也需要结合编程式验证。
简单说一下如何我是使用的:
1.需要的jar包:hibernate-validator-4.3.0.Final.jar,validation-api-1.0.0.GA.jar,(jboss-logging-3.1.0.CR2.jar这个好象不需要)
2.配置:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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"
default-autowire="byName" >
<import resource="classpath:/spring/propertyFileConfigurer.xml" />
<!-- 自动搜索@Controller标注的类 -->
<context:component-scan base-package="com.**.web"/>
<!-- <mvc:annotation-driven /> -->
<bean id="tplViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/tpl/"/>
<property name="viewNames">
<list>
<value>*.html</value>
</list>
</property>
<property name="order" value="1"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/pages/${mc.viewResolver.basename}/"/>
<property name="suffix" value=".jsp"/>
<property name="order" value="2"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer" ref="webBindingInitializer"/>
</bean>
<bean id="mappingJacksonJsonView" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</property>
</bean>
<bean id="webBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService"/>
<property name="validator" ref="validator"/>
</bean>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
<!-- 如果不加默认,则使用classpath下的 ValidationMessages.properties -->
<!-- <property name="validationMessageSource" ref="messageSource"/> -->
</bean>
<!-- ①注册ConversionService -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
</list>
</property>
</bean>
3.写验证类:这是用于个人注册时的验证类
public class UsersRegValidator implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
//columns START
private String loginName;
private String password;
private String email;
private String mobile;
private String nickname;
public void setLoginName(String value) {
this.loginName = value;
}
@NotEmpty(message="{loginName.not.empty}")
@Length(min = 4, max = 12, message = "{loginName.length}")
@Pattern(regexp = Constants.REGEXP_CODE, message = "${formatter.loginName}")
@LoginNameOnly
public String getLoginName() {
return this.loginName;
}
public void setPassword(String value) {
this.password = value;
}
@NotEmpty(message="{password.not.empty}")
@Length(min = 5, max = 12, message = "{password.length}")
public String getPassword() {
return this.password;
}
public void setEmail(String value) {
this.email = value;
}
@NotEmpty(message="{email.not.empty}")
@Length(min = 1, max = 60, message = "{email.length}")
@Email(message="{formatter.email}")
@EmailOnly
public String getEmail() {
return this.email;
}
public void setMobile(String value) {
this.mobile = value;
}
@NotEmpty(message="{mobile.not.empty}")
@Pattern(regexp = Constants.REGEXP_MOBILE, message = "${formatter.mobile}")
public String getMobile() {
return this.mobile;
}
public void setNickname(String value) {
this.nickname = value;
}
@NotEmpty(message="{nickname.not.empty}")
@Length(min = 2, max = 10, message = "{nickname.length}")
@Pattern(regexp = Constants.REGEXP_SPECIALCHARACTER, message = "${validate.specialCharacter}")
public String getNickname() {
return this.nickname;
}
}
4.在controller中 使用进行服务端验证
/**
* 个人注册.--注册
**/
@RequestMapping
public String saveRegInfo(@Valid @ModelAttribute("frontValidate") final UsersRegValidator frontValidate, Errors errors,final Users users) throws Exception {
if(errors.hasErrors()) {
return "admin/validate";
}
return "";
}
5.显示验证结果的界面:这里显示结果的界面不是注册的界面,是单独的一个通用界面,
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="/commons/meta_error.jsp"%>
<html>
<head>
<title>Error Page</title>
</head>
<body>
<div class="wrap" id="frontContent" >
<!--header-->
<jsp:include page="/pages/guanlin/guanlincommon/header.jsp" flush="true">
<jsp:param name="modelname" value="数据验证失败" />
</jsp:include>
<!--header End-->
<div class="main">
<h3>对不起,数据验证失败</h3>
<form:form commandName="frontValidate">
<form:errors path="*" cssStyle="color:red"></form:errors><br/>
</form:form>
<br/>
<input id="subBtn" type="button" onclick="history.back();" value="返回"/>
<br/><br/>
</div>
</div>
</body>
</html>
到此,服务端验证就结束了。其实很简单。
相关推荐
spring mvc服务端表单验证实例 能跑起来看效果 lib齐全 希望对您的学习有帮助
服务端验证:Blade Validator 任务调度:Spring Task 持久层框架:beetlsql 模板引擎:beetl 数据库连接池:Alibaba Druid 缓存框架:Ehcache 日志管理:SLF4J、LOGBACKUP 工具类:Apache Commons、FastJson...
包含权限验证、springmvc+mybatis的使用,以及mybatis的分页查询等。改例子可以作为一个手机后台服务端的demo,对刚入门的同学有一定的帮助
SpringMVC自身对数据在服务端的校验有一个比较好的支持,它能将我们提交到服务端的数据按照我们事先的约定进行数据有效性验证,对于不合格的数据信息SpringMVC会把它保存在错误对象中,这些错误信息我们也可以通过...
Maven+Spring MVC +Hibernate Validate(服务端验证) 增删改 ajax请求 文件上传
视图框架:Spring MVC、服务端验证:Blade Validator 任务调度:Spring Task、持久层框架:beetlsql、模板引擎:beetl 数据库连接池:Alibaba Druid、缓存框架:Ehcache、日志管理:SLF4J、LOGBACKUP 工具类:...
整合spring+springWebMVC+cas客户端,亲自验证可用,连接的cas服务端是cas4.2.7,下载后在使用中若有问题,请联系我
使用JavaEmail插件实现邮件发送,记得需要开启SSl验证 ![Image text](https://gitee.com/362330721/jeeplatform/raw/master/screenshot/发送邮件.png) ### OA管理系统(待开发) 接入CAS Server实现单点登录 ### CMS...
如果服务端是3.X的版本,你用2的客户端是用户密码是验证不通过的。所以需要选择对应的客户端。 mongo的配置文件路劲:\resources\config\mongoDb.xml (此文件中 注释的配置为2.X版本的配置。 不是注释的代码为3.X的...
用的技术是jQuery异步验证和struts2(其实springMVC也是一样的道理,都只是一个控制器). 表单: 代码如下: <input class=”width150″ maxlength=”32″ type=”text” id=”dept_name” name=”dept.dept_name...
项目概述:本移动端康养App后端采用Java语言开发,基于Vue前端技术与SSM(Spring、SpringMVC、MyBatis)后端框架构建。项目共包含323个文件,其中主要包括88个图片资源文件(png、jpg),84个class文件,42个Java...
代码生成前端界面、底层代码(spring mvc、mybatis、Spring boot、Spring Cloud、微服务的生成)、安全框架、视图框架、服务端验证、任务调度、持久层框架、数据库连接池、缓存框架、日志管理、IM等核心技术。...
代码生成前端界面、底层代码(spring mvc、mybatis、Spring boot、Spring Cloud、微服务的生成)、安全框架、视图框架、服务端验证、任务调度、持久层框架、数据库连接池、缓存框架、日志管理、IM等核心技术。...
服务端使用springMVC的方案进行处理。 2 Html代码 <form id="myForm"> <input type="file" id="u_photo" name="u_photo" /> <input type="button" id="submit-btn" value="上传" /> </form&...
其中node1跟node2都是采用spring + springMVC + mybatis 框架,使用maven做项目管理 ## cas集成说明 1.首先采用的是查数据库的方式来校验用户身份的,在cas/WEB-INF/deployerConfigContext.xml中第135行构建了这个...
2.启动redis服务端,修改ssm-rs\resources\spring-config目录下spring-shiro.xml中redis的连接信息,没设置密码的话auth留空 2.创建数据库train_db并执行根目录下的train_db.sql数据库脚本 3.进入到ssm-...
用户登录注册功能:页面验证账号和密码是否合法,然后进行服务端的用户是否存在验证,通过就登陆成功,否则提示用户注册。 公告栏功能:用户可以他通过公告栏看到网站的通知和信息。 环保资讯功能:用户可以通过...
代码生成前端界面、底层代码(spring mvc、mybatis、Spring boot、Spring Cloud、微服务的生成)、安全框架、视图框架、服务端验证、任务调度、持久层框架、数据库连接池、缓存框架、日志管理、IM等核心技术。...