protected Map<String, Object> buildScope(Request request) { Map<String, Object> scope = new HashMap<String, Object>(); List<String> availableScopes = request.getAvailableScopes(); for (int i = availableScopes.size() - 1; i >= 0; --i) { scope.putAll(request.getContext(availableScopes.get(i))); } return scope; }
@Override public ViewPreparer getPreparer(String name, Request context) { WebApplicationContext webApplicationContext = (WebApplicationContext) context.getContext("request").get(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE); if (webApplicationContext == null) { webApplicationContext = (WebApplicationContext) context .getContext("application") .get(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); if (webApplicationContext == null) { throw new IllegalStateException( "No WebApplicationContext found: no ContextLoaderListener registered?"); } } return getPreparer(name, webApplicationContext); }
@Override public void render(String templateName, Request request) throws IOException { // Compile template after reading file from file system. String templatePath = new StringBuilder(prefix).append("/").append(templateName).append(suffix).toString(); if (!templateMap.containsKey(templateName)) templateMap.put(templateName, Handlebars.compile(getFileContent(templatePath))); // Render template Template compiledTemplate = templateMap.get(templateName); compiledTemplate.apply(buildScope(request), request.getWriter()); }