日期:2014-05-16 浏览次数:20400 次
import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; import javax.servlet.http.HttpServletRequest; import jetbrick.template.JetAnnotations.Functions; import jetbrick.template.JetContext; import jetbrick.template.runtime.JetPageContext; import jetbrick.template.web.JetWebContext; import org.springframework.context.MessageSource; import org.springframework.context.NoSuchMessageException; import org.springframework.validation.BindingResult; import org.springframework.validation.Errors; import org.springframework.validation.FieldError; import org.springframework.validation.ObjectError; import org.springframework.web.servlet.support.RequestContextUtils; @Functions public class SpringFunctions { private static final List<String> EMPTY_STRING_LIST = Collections.unmodifiableList(new ArrayList<String>()); public static List<String> errors(JetPageContext ctx, String filedName) { HttpServletRequest request = (HttpServletRequest) ctx.getContext().get(JetWebContext.REQUEST); Errors errors = findErrors(ctx); if (errors == null) { System.out.println("没有找到Errors"); return EMPTY_STRING_LIST; } List<FieldError> fes = errors.getFieldErrors(filedName); List<String> msgs = new ArrayList<String>(0); for(FieldError fe : fes) { String[] codes = fe.getCodes(); String defaultMsg = fe.getDefaultMessage(); Object[] args = fe.getArguments(); Locale locale = findLocale(request); MessageSource ms = findMessageSource(request); if (codes == null || codes.length == 0 || ms == null) { msgs.add(defaultMsg); } else { String msg = null; for (int i = 0; i < codes.length; i ++) { try { msg = ms.getMessage(codes[i], args, locale); } catch (NoSuchMessageException e) { // 忽略 } if (msg == null) { msg = defaultMsg; } } msgs.add(msg); } } return Collections.unmodifiableList(msgs); } public static List<String> errors(JetPageContext ctx) { HttpServletRequest request = (HttpServletRequest) ctx.getContext().get(JetWebContext.REQUEST); Errors errors = findErrors(ctx); if (errors == null) { return EMPTY_STRING_LIST; } List<ObjectError> oes = errors.getGlobalErrors(); List<String> msgs = new ArrayList<String>(0); for (ObjectError oe : oes) { String[] codes = oe.getCodes(); String defaultMsg = oe.getDefaultMessage(); Object[] args = oe.getArguments(); Locale locale = findLocale(request); MessageSource ms = findMessageSource(request); if (codes == null || codes.length == 0 || ms == null) { msgs.add(defaultMsg); } else { String msg = null; for (int i = 0; i < codes.length; i ++) { try { msg = ms.getMessage(codes[i], args, locale); } catch (NoSuchMessageException e) { // 忽略 } if (msg == null) { msg = defaultMsg; } } msgs.add(msg); } } return Collections.unmodifiableList(msgs); } @SuppressWarnings("unchecked") private static Errors findErrors(JetPageContext ctx) { try { JetContext jetContext = ctx.getContext(); Field[] fileds = JetContext.class.getDeclaredFields(); Field f = null; for (Field field : fileds) { if (field.getName().equals("context")) { f = field; break; } } if (f == null) { return null; } f.setAccessible(true); Map<String, Object> context = (Map<String, Object>) f.get(jetContext); Set<String> keyset = context.keySet(); String key = null; for (String k : keyset) { if (k.startsWith(BindingResult.MODEL_KEY_PREFIX)) { key = k; break; } } if (key == null) { return null; } return (Errors) jetContext.g