在?项目中使用#springMessage进行错误码解析为错误消息,可是在特定的场景下出现了
????? $springMacroRequestContext.getMessage($code)
?
#springMessage????? 是在org/springframework/web/servlet/view/velocity/spring.vm 文件中定义了一个宏。其内容是:
?
#** * springMessage * * Macro to translate a message code into a message. *# #macro( springMessage $code )$springMacroRequestContext.getMessage($code)#end?
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";?
if (this.exposeSpringMacroHelpers) { 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. model.put(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, new RequestContext(request, response, getServletContext(), model)); }?至此我们可以发现?这个对象就是org.springframework.web.servlet.support.RequestContext?对象。
/** * Retrieve the message for the given code, using the "defaultHtmlEscape" setting. * @param code code of the message * @return the message * @throws org.springframework.context.NoSuchMessageException if not found */ public String getMessage(String code) throws NoSuchMessageException { return getMessage(code, null, isDefaultHtmlEscape()); }?
原文:http://zhangwei-david.iteye.com/blog/2158126