@RequestMapping("/decorator") public void decorator(HttpServletRequest request, HttpServletResponse response) throws Exception { AbstractTemplateView view = (AbstractTemplateView)viewResolver.resolveViewName("decorator", localeResolver.resolveLocale(request)); //因为此请求是Sitemesh forward进来的,如果不做下面两个Override配置的话会重复设置Model,而Spring MVC碰到重复参数名会抛异常 //详细逻辑请阅读 {@link AbstractTemplateView} 的 renderMergedOutputModel方法 //启用spring.freemarker.expose-request-attributes=true时则需要启用下面的代码 //view.setAllowRequestOverride(true); //启用spring.freemarker.expose-session-attributes=true时则需要启用下面的代码 //view.setAllowSessionOverride(true); view.setExposeSpringMacroHelpers(false); view.render(null, request, response); }
@Override public Object call(Object[] paras, Context ctx) { RequestContext requestContext = (RequestContext) ctx .getGlobal(AbstractTemplateView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE); Assert.notNull(requestContext); Assert.notNull(paras); Assert.isTrue(paras.length > 0); String code = (String) paras[0]; Object[] args = paras.length > 1 ? Arrays.copyOfRange(paras, 1, paras.length) : null; return requestContext.getMessage(code, args); }
private void additionalVariableProcessing(ConversionService conversionService, Map<String, Object> variables) { RequestContext requestContext = (RequestContext) variables.get(AbstractTemplateView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE); variables.put(SpringContextVariableNames.SPRING_REQUEST_CONTEXT, requestContext); ThymeleafEvaluationContext evaluationContext = new ThymeleafEvaluationContext(applicationContext, conversionService); variables.put(ThymeleafEvaluationContext.THYMELEAF_EVALUATION_CONTEXT_CONTEXT_VARIABLE_NAME, evaluationContext); }
protected <V extends AbstractTemplateView> V initialize (V view) { super.initialize (view); view.setAllowRequestOverride (allowRequestOverride); view.setAllowSessionOverride (allowSessionOverride); view.setExposeRequestAttributes (exposeRequestAttributes); view.setExposeSessionAttributes (exposeSessionAttributes); view.setExposeSpringMacroHelpers (exposeSpringMacroHelpers); return view; }
private void doAdditionalVariableProcessing(ITest test, Locale locale, Map<String, Object> variables) { servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext); RequestContextHolder.setRequestAttributes(webRequest); ConversionService conversionService = getConversionService(); RequestContext requestContext = new RequestContext(request, response, servletContext, variables); variables.put(AbstractTemplateView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, requestContext); additionalVariableProcessing(conversionService, variables); initializeBindingResults(test, conversionService, locale, variables); injectJawrProperties(test); }