#springMessage("xxx")处理国际化:
spring处理国际化的方式能够很特别,由于可使用这个标记.
这个标记是在spring整合velocity模版后才能使用.这个模版在:
org.springframework.web.servlet\src\main\java\org\springframework\web\servlet\view\velocity\spring.vm
velocity模版中第一个宏就是springMessage标记,还有其余的标记,若是用velocity的话,确实是很方便.
#macro( springMessage $code )$springMacroRequestContext.getMessage($code)#end
在此又有疑惑.由于这个宏用到了$springMacroRequestContext这个东西又是怎么回事呢?
原来这个变量在org.springframework.web.servlet.view.AbstractTemplateView.java中被定义并赋值java
public abstract class AbstractTemplateView extends AbstractUrlBasedView { /** * Variable name of the RequestContext instance in the template model, * available to Spring's macros: e.g. for creating BindStatus objects. */ public static final String SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE = "springMacroRequestContext"; //...略 //从字面上就可以理解了,渲染宏并输出到model protected final void renderMergedOutputModel( Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { //若是配置VelocityViewResolver模版时将此属性设置为true,那么将把request中的属性值放到model中 if (this.exposeRequestAttributes) { for (Enumeration en = request.getAttributeNames(); en.hasMoreElements();) { String attribute = (String) en.nextElement(); if (model.containsKey(attribute) && !this.allowRequestOverride) { throw new ServletException("Cannot expose request attribute '" + attribute + "' because of an existing model object of the same name"); } Object attributeValue = request.getAttribute(attribute); if (logger.isDebugEnabled()) { logger.debug("Exposing request attribute '" + attribute + "' with value [" + attributeValue + "] to model"); } model.put(attribute, attributeValue); } } //若是配置VelocityViewResolver模版时将此属性设置为true,那么将把session中的属性值放到model中 if (this.exposeSessionAttributes) { HttpSession session = request.getSession(false); if (session != null) { for (Enumeration en = session.getAttributeNames(); en.hasMoreElements();) { String attribute = (String) en.nextElement(); if (model.containsKey(attribute) && !this.allowSessionOverride) { throw new ServletException("Cannot expose session attribute '" + attribute + "' because of an existing model object of the same name"); } Object attributeValue = session.getAttribute(attribute); if (logger.isDebugEnabled()) { logger.debug("Exposing session attribute '" + attribute + "' with value [" + attributeValue + "] to model"); } model.put(attribute, attributeValue); } } } //若是宏能够被使用,从VelocityViewResolver的父类AbstractTemplateViewResolver //能够得知exposeSpringMacroHelpers默认为true if (this.exposeSpringMacroHelpers) { //若是model中存在,那么便抛出一个异常,不然 if (model.containsKey(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE)) { throw new ServletException( "Cannot expose bind macro helper '" + SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE + "' because of an existing model object of the same name"); } // Expose RequestContext instance for Spring macros. // 翻译一下:将RequestContext实例对象暴露为宏变量,说直接就是给SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE赋值 model.put(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, new RequestContext(request, response, getServletContext(), model)); } applyContentType(response); renderMergedTemplateModel(model, request, response);//渲染velocity模版,子类VelocityView会实现 }
由此在页面中可使用#springMessage("xxx")来获取资源的代码值.固然在代码中能够看到其实这个宏就是RequestContext对象.
灵活运用吧.web