SpringMVC杂记(十四) Ajax方式的JSR303认证

阅读更多
自己定义一个Exception,用来表示数据绑定失败
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.validation.BindingResult;

import ying.vehicle.commons.validation.BindingResultUtils;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonAutoDetect(fieldVisibility = Visibility.NONE, getterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE)
public class DataBindingException extends RuntimeException {

	private static final long serialVersionUID = 4886698281191876208L;

	@JsonProperty
	private boolean success = true;
	
	@JsonProperty
	private List globalErrors = new ArrayList();
	
	@JsonProperty
	private Map> fieldErrors = new HashMap>();

	public DataBindingException(BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			success = false;
			globalErrors = BindingResultUtils.getGlobalErrors(bindingResult);
			fieldErrors  = BindingResultUtils.getFieldErros(bindingResult);
		} 
	}

	// getter & setter
	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public List getGlobalErrors() {
		return globalErrors;
	}

	public void setGlobalErrors(List globalErrors) {
		this.globalErrors = globalErrors;
	}

	public Map> getFieldErrors() {
		return fieldErrors;
	}

	public void setFieldErrors(Map> fieldErrors) {
		this.fieldErrors = fieldErrors;
	}
	
}

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;

public class BindingResultUtils {

	private BindingResultUtils() {
	}
	
	public static Object getErrorMessages(BindingResult bindingResult) {
		if (bindingResult == null) {
			return null;
		}

		Map root = new HashMap();
		if (bindingResult.hasErrors()) {
			root.put("success", false);
			root.put("globalErrors", getGlobalErrors(bindingResult));
			root.put("fieldErrors",  getFieldErros(bindingResult));
		} else {
			root.put("success", true);
		}
		
		return root;
	}

	public static List getGlobalErrors(BindingResult bindingResult) {
		List globalErrors = new ArrayList();
		for (ObjectError oe : bindingResult.getGlobalErrors()) {
			globalErrors.add(oe.getDefaultMessage());
		}
		return globalErrors;
	}
	
	public static Map> getFieldErros(BindingResult bindingResult) {
		Map> fieldErrors = new HashMap>();
		for (FieldError fe : bindingResult.getFieldErrors()) {
			String f = fe.getField();
			
			if (fieldErrors.get(f) != null) {
				fieldErrors.get(f).add(fe.getDefaultMessage());
			} else {
				List list = new LinkedList();
				list.add(fe.getDefaultMessage());
				fieldErrors.put(f, list);
			}
		}
		return fieldErrors;
	}
}


spring-mvc 3.2.x有一个新标注 @ControllerAdvice 在异常处理的场合,很好用
@ControllerAdvice
public class ApplicationControllerAdvice {

	@ResponseStatus(HttpStatus.OK)
	@ExceptionHandler(DataBindingException.class)
	public @ResponseBody Object handleDataBindingException(NativeWebRequest request, DataBindingException ex) {
		return ex;
	}

}




	com.fasterxml.jackson.jaxrs
	jackson-jaxrs-xml-provider
	2.2.2

你可能感兴趣的:(ajax,binding,spring-mvc,jsr303)