Java tutorial
/** * Copyright © 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.dcampus.common.web; import java.beans.PropertyEditorSupport; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.validation.ConstraintViolationException; import javax.validation.Validator; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.lang3.StringEscapeUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ui.Model; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.dcampus.common.beanvalidator.BeanValidators; import com.dcampus.common.util.DateUtils; /** * ? * @author ThinkGem * @version 2013-3-23 */ public abstract class BaseController { /** * ?Bean */ @Autowired protected Validator validator; /** * ??? * @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; } /** * ??? * @param object ? * @param groups ? * @return ??true?? flash message */ protected boolean beanValidator(RedirectAttributes redirectAttributes, Object object, Class<?>... groups) { try { BeanValidators.validateWithException(validator, object, groups); } catch (ConstraintViolationException ex) { List<String> list = BeanValidators.extractPropertyAndMessageAsList(ex, ": "); list.add(0, "??"); addMessage(redirectAttributes, list.toArray(new String[] {})); return false; } return true; } /** * Model? * @param message */ protected void addMessage(Model model, String... messages) { List<String> messageList = new ArrayList<String>(); for (int i = 0; i < messages.length; i++) { messageList.add(messages[i]); } model.addAttribute("message", messageList); } /** * Flash? * @param message */ protected void addMessage(RedirectAttributes redirectAttributes, String... messages) { List<String> messageList = new ArrayList<String>(); for (int i = 0; i < messages.length; i++) { messageList.add(messages[i]); } // redirectAttributes.addAttribute("message", sb.toString()); redirectAttributes.addFlashAttribute("message", messageList); } protected void addMessage(RedirectAttributes redirectAttributes, List<String> messages) { redirectAttributes.addFlashAttribute("message", messages); } protected void populate(Object obj, HttpServletRequest request) { try { BeanUtils.populate(obj, request.getParameterMap()); } catch (Exception e) { // e.printStackTrace(); } } /** * ?? * 1. ?StringHTML?XSS * 2. Date?String */ @InitBinder protected void initBinder(WebDataBinder binder) { // String??StringHTML?XSS binder.registerCustomEditor(String.class, new PropertyEditorSupport() { @Override public void setAsText(String text) { setValue(text == null ? null : StringEscapeUtils.escapeHtml4(text.trim())); } @Override public String getAsText() { Object value = getValue(); return value != null ? value.toString() : ""; } }); // Date ? binder.registerCustomEditor(Date.class, new PropertyEditorSupport() { @Override public void setAsText(String text) { setValue(DateUtils.parseDate(text)); } }); } }