zoukankan      html  css  js  c++  java
  • 浅析调用JSR303的validate方法, 验证失败时抛出ConstraintViolationException

    废话不多说,直接进入正题:如何使用JSR303的validate,进行数据校验,失败后直接抛出异常加入流转信息中,并在form页面提示出来。

    首先我们为了启用验证,需要向

    项目中添加Bean验证的实现。本列选择Hibernate Validator框架来提供验证功能。可以像下面的示例那样将该项目作为一个Maven依赖添加到当前项目中。此外,Hibernate Validator会将Bean Validation API作为一个船只依赖添加到项目中。

    <dependency>  
        <groupId>org.hibernate</groupId>  
        <artifactId>hibernate-validator</artifactId>  
        <version>5.1.1.Final</version>  
    </dependency>  
    

      

    在表单页面引入如下标签:

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    

    然后使用该标签下的form:form进行数据验证的绑定

    <form:form id="inputForm" modelAttribute="article" action="${ctx}/cms/article/save" method="post" class="form-horizontal">
    		
    		<div class="control-group">
    			<label class="control-label">摘要:</label>
    			<div class="controls">
    				<form:textarea path="description" htmlEscape="false" rows="4" maxlength="200" class="input-xxlarge"/>
    			</div>
    		</div>
    		<div class="form-actions">
    			<shiro:hasPermission name="cms:article:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/> </shiro:hasPermission>
    			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
    		</div>
    	</form:form>
    

      如上我只是简单举了一个字段为description(简介/描述)的数据验证例子 其实这里的maxlength="200",我们完全可以忽略它,因为前段的验证始终是不安全的,而且我们在后台进行的验证因为整合了jsr303变的异常的简洁,我们只需要在需要验证的

    表单实体的get方法上加上一行注解:

    @Length(min=0, max=5)
    	public String getDescription() {
    		return description;
    	}
    

      //这行注解的意思是最小长度是0,最大长度是5

    然后在控制层写上验证失败加入流转信息并返回form页的代码:

    	@RequestMapping(value = "save")
    	public String save(Article article, Model model, RedirectAttributes redirectAttributes) {
    		if (!beanValidator(model, article)){
    			return form(article, model);
    		}
    		articleService.save(article);
    		addMessage(redirectAttributes, "保存文章'" + StringUtils.abbr(article.getTitle(),50) + "'成功");
    		String categoryId = article.getCategory()!=null?article.getCategory().getId():null;
    		return "redirect:" + adminPath + "/cms/article/?repage&category.id="+(categoryId!=null?categoryId:"");
    	}
    

      我们进入beanValidator(model,article)这个方法一探究竟:

    /**
    	 * 服务端参数有效性验证
    	 * @param object 验证的实体对象
    	 * @param groups 验证组
    	 * @return 验证成功:返回true;严重失败:将错误信息添加到 message 中
    	 */
    	protected boolean beanValidator(Model model, Object object, Class<?>... groups) {
    		try{
    			BeanValidators.validateWithException(validator, object, groups);
    		}catch(ConstraintViolationException ex){
    			List<String> list = BeanValidators.extractPropertyAndMessageAsList(ex, ": ");
    			list.add(0, "数据验证失败:");
    			addMessage(model, list.toArray(new String[]{}));
    			return false;
    		}
    		return true;
    	}
    

      

    /**
     * JSR303 Validator(Hibernate Validator)工具类.
     * 
     * ConstraintViolation中包含propertyPath, message 和invalidValue等信息.
     * 提供了各种convert方法,适合不同的i18n需求:
     * 1. List<String>, String内容为message
     * 2. List<String>, String内容为propertyPath + separator + message
     * 3. Map<propertyPath, message>
     * 
     * 详情见wiki: https://github.com/springside/springside4/wiki/HibernateValidator
     * @author calvin
     * @version 2013-01-15
     */
    public class BeanValidators {
    
    	/**
    	 * 调用JSR303的validate方法, 验证失败时抛出ConstraintViolationException.
    	 */
    	@SuppressWarnings({ "unchecked", "rawtypes" })
    	public static void validateWithException(Validator validator, Object object, Class<?>... groups)
    			throws ConstraintViolationException {
    		Set constraintViolations = validator.validate(object, groups);
    		if (!constraintViolations.isEmpty()) {
    			throw new ConstraintViolationException(constraintViolations);
    		}
    	}
    
    /**
    	 * 辅助方法, 转换ConstraintViolationException中的Set<ConstraintViolations>为List<propertyPath +separator+ message>.
    	 */
    	public static List<String> extractPropertyAndMessageAsList(ConstraintViolationException e, String separator) {
    		return extractPropertyAndMessageAsList(e.getConstraintViolations(), separator);
    	}
    
    }
    

      

      

    /**
    	 * 添加Model消息
    	 * @param message
    	 */
    	protected void addMessage(Model model, String... messages) {
    		StringBuilder sb = new StringBuilder();
    		for (String message : messages){
    			sb.append(message).append(messages.length>1?"<br/>":"");
    		}
    		model.addAttribute("message", sb.toString());
    	}
    

      

    最后我们要做的就是在form页面把model中的错误信息展示出来就行了:

    <script type="text/javascript">top.$.jBox.closeTip();</script>
    <c:if test="${not empty content}">
    	<c:if test="${not empty type}"><c:set var="ctype" value="${type}"/></c:if><c:if test="${empty type}"><c:set var="ctype" value="${fn:indexOf(content,'失败') eq -1?'success':'error'}"/></c:if>
    	<div id="messageBox" class="alert alert-${ctype} hide"><button data-dismiss="alert" class="close">×</button>${content}</div> 
    	<script type="text/javascript">if(!top.$.jBox.tip.mess){top.$.jBox.tip.mess=1;top.$.jBox.tip("${content}","${ctype}",{persistent:true,opacity:0});$("#messageBox").show();}</script>
    </c:if>
    

      例子展示如下:

      

    这种验证方法是每次都会向服务器发送一次请求,如果一些简单的验证不需要向后台区请求,我们可以使用自定义的validate在前端完成简单的数据验证:

    $("#inputForm").validate({
    				submitHandler: function(form){
                        if ($("#categoryId").val()==""){
                            $("#categoryName").focus();
                            top.$.jBox.tip('请选择归属栏目','warning');
                        }else if (CKEDITOR.instances.content.getData()=="" && $("#link").val().trim()==""){
                            top.$.jBox.tip('请填写正文','warning');
                        }else{
                            loading('正在提交,请稍等...');
                            form.submit();
                        }
    				},
    				errorContainer: "#messageBox",
    				errorPlacement: function(error, element) {
    					$("#messageBox").text("输入有误,请先更正。");
    					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
    						error.appendTo(element.parent().parent());
    					} else {
    						error.insertAfter(element);
    					}
    				}
    			});
    

      

  • 相关阅读:
    让x86的android模拟器能模拟arm架构系统
    婴儿补充微量元素
    asterisk 能打电话的配置
    SIP协议错误代码大全
    asterisk错误排查
    asterisk帮助与国内论坛
    win10 只要打开文件对话框就卡死解决方法
    分享到朋友圈实现
    跳转前暂停几秒js如何实现
    Github css加载失败,样式混乱解决办法
  • 原文地址:https://www.cnblogs.com/fengwenzhee/p/8422362.html
Copyright © 2011-2022 走看看