/** * Process the model map by merging it with the FreeMarker template. * Output is directed to the servlet response. * <p>This method can be overridden if custom behavior is needed. */ @Override protected void renderMergedTemplateModel( Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { exposeHelpers(model, request); doRender(model, request, response); }
/** * Invoked on startup. Looks for a single FreeMarkerConfig bean to * find the relevant Configuration for this factory. * <p>Checks that the template for the default Locale can be found: * FreeMarker will check non-Locale-specific templates if a * locale-specific one is not found. * @see freemarker.cache.TemplateCache#getTemplate */ @Override protected void initServletContext(ServletContext servletContext) throws BeansException { if (getConfiguration() != null) { this.taglibFactory = new TaglibFactory(servletContext); } else { FreeMarkerConfig config = autodetectConfiguration(); setConfiguration(config.getConfiguration()); this.taglibFactory = config.getTaglibFactory(); } GenericServlet servlet = new GenericServletAdapter(); try { servlet.init(new DelegatingServletConfig()); } catch (ServletException ex) { throw new BeanInitializationException("Initialization of GenericServlet adapter failed", ex); } this.servletContextHashModel = new ServletContextHashModel(servlet, getObjectWrapper()); }
exposeModelAsRequestAttributes(model, request); SimpleHash fmModel = buildTemplateModel(model, request, response); processTemplate(getTemplate(locale), fmModel, response);
/** * Retrieve the FreeMarker template for the given locale, * to be rendering by this view. * <p>By default, the template specified by the "url" bean property * will be retrieved. * @param locale the current locale * @return the FreeMarker template to render * @throws IOException if the template file could not be retrieved * @see #setUrl * @see #getTemplate(String, java.util.Locale) */ protected Template getTemplate(Locale locale) throws IOException { String url = getUrl(); Assert.state(url != null, "'url' not set"); return getTemplate(url, locale); }
model.put("options", names.values()); FreeMarkerView view = new FreeMarkerView(); view.setBeanName("myView"); view.setUrl("tmp.ftl"); view.setExposeSpringMacroHelpers(false); view.setConfiguration(config); view.setServletContext(new MockServletContext()); view.render(model, request, response);
/** * Invoked on startup. Looks for a single FreeMarkerConfig bean to * find the relevant Configuration for this factory. * <p>Checks that the template for the default Locale can be found: * FreeMarker will check non-Locale-specific templates if a * locale-specific one is not found. * @see freemarker.cache.TemplateCache#getTemplate */ @Override protected void initServletContext(ServletContext servletContext) throws BeansException { if (getConfiguration() != null) { this.taglibFactory = new TaglibFactory(servletContext); } else { FreeMarkerConfig config = autodetectConfiguration(); setConfiguration(config.getConfiguration()); this.taglibFactory = config.getTaglibFactory(); } GenericServlet servlet = new GenericServletAdapter(); try { servlet.init(new DelegatingServletConfig()); } catch (ServletException ex) { throw new BeanInitializationException("Initialization of GenericServlet adapter failed", ex); } this.servletContextHashModel = new ServletContextHashModel(servlet, getObjectWrapper()); }
/** * Build a FreeMarker template model for the given model Map. * <p>The default implementation builds a {@link AllHttpScopesHashModel}. * @param model the model to use for rendering * @param request current HTTP request * @param response current servlet response * @return the FreeMarker template model, as a {@link SimpleHash} or subclass thereof */ protected SimpleHash buildTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) { AllHttpScopesHashModel fmModel = new AllHttpScopesHashModel(getObjectWrapper(), getServletContext(), request); fmModel.put(FreemarkerServlet.KEY_JSP_TAGLIBS, this.taglibFactory); fmModel.put(FreemarkerServlet.KEY_APPLICATION, this.servletContextHashModel); fmModel.put(FreemarkerServlet.KEY_SESSION, buildSessionModel(request, response)); fmModel.put(FreemarkerServlet.KEY_REQUEST, new HttpRequestHashModel(request, response, getObjectWrapper())); fmModel.put(FreemarkerServlet.KEY_REQUEST_PARAMETERS, new HttpRequestParametersHashModel(request)); fmModel.putAll(model); return fmModel; }
@Override protected void exposeHelpers(Map<String, Object> model, HttpServletRequest request) throws Exception { exposeAttribute(model, request); super.exposeHelpers(model, request); }
exposeModelAsRequestAttributes(model, request); SimpleHash fmModel = buildTemplateModel(model, request, response); processTemplate(getTemplate(locale), fmModel, response);
@Override public Template getTemplate(Locale locale) throws IOException { return super.getTemplate(locale); }
@Override protected AbstractUrlBasedView buildView(String viewName) throws Exception { FreeMarkerView view = (FreeMarkerView) super.buildView(viewName); if (null != configuration) { view.setConfiguration(configuration); } return view; }
/** * Obtain the FreeMarker configuration for actual use. * @return the FreeMarker configuration (never {@code null}) * @throws IllegalStateException in case of no Configuration object set * @since 5.0 */ protected Configuration obtainConfiguration() { Configuration configuration = getConfiguration(); Assert.state(configuration != null, "No Configuration set"); return configuration; }
@Test public void freeMarkerViewResolver() throws Exception { MockServletContext sc = new MockServletContext(); FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); configurer.setConfiguration(new TestConfiguration()); configurer.setServletContext(sc); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); wac.getBeanFactory().registerSingleton("configurer", configurer); wac.refresh(); FreeMarkerViewResolver vr = new FreeMarkerViewResolver("prefix_", "_suffix"); vr.setApplicationContext(wac); View view = vr.resolveViewName("test", Locale.CANADA); assertEquals("Correct view class", FreeMarkerView.class, view.getClass()); assertEquals("Correct URL", "prefix_test_suffix", ((FreeMarkerView) view).getUrl()); view = vr.resolveViewName("non-existing", Locale.CANADA); assertNull(view); view = vr.resolveViewName("redirect:myUrl", Locale.getDefault()); assertEquals("Correct view class", RedirectView.class, view.getClass()); assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl()); view = vr.resolveViewName("forward:myUrl", Locale.getDefault()); assertEquals("Correct view class", InternalResourceView.class, view.getClass()); assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl()); }
super.doRender(model, request, response);
@Override protected SimpleHash buildTemplateModel( Map<String, Object> model, HttpServletRequest request, HttpServletResponse response ) { PortalConfig config = AppBeans.get(Configuration.class).getConfig(PortalConfig.class); SimpleHash context = super.buildTemplateModel(model, request, response); SecurityContext securityContext = AppContext.getSecurityContext(); if (securityContext != null) context.put("userSession", securityContext.getSession()); context.put("messages", messages); context.put("message", new MessageMethod()); context.put("theme", config.getTheme()); return context; }
/** * Build a FreeMarker template model for the given model Map. * <p>The default implementation builds a {@link AllHttpScopesHashModel}. * @param model the model to use for rendering * @param request current HTTP request * @param response current servlet response * @return the FreeMarker template model, as a {@link SimpleHash} or subclass thereof */ protected SimpleHash buildTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) { AllHttpScopesHashModel fmModel = new AllHttpScopesHashModel(getObjectWrapper(), getServletContext(), request); fmModel.put(FreemarkerServlet.KEY_JSP_TAGLIBS, this.taglibFactory); fmModel.put(FreemarkerServlet.KEY_APPLICATION, this.servletContextHashModel); fmModel.put(FreemarkerServlet.KEY_SESSION, buildSessionModel(request, response)); fmModel.put(FreemarkerServlet.KEY_REQUEST, new HttpRequestHashModel(request, response, getObjectWrapper())); fmModel.put(FreemarkerServlet.KEY_REQUEST_PARAMETERS, new HttpRequestParametersHashModel(request)); fmModel.putAll(model); return fmModel; }
/** * Retrieve the FreeMarker template for the given locale, * to be rendering by this view. * <p>By default, the template specified by the "url" bean property * will be retrieved. * @param locale the current locale * @return the FreeMarker template to render * @throws IOException if the template file could not be retrieved * @see #setUrl * @see #getTemplate(String, java.util.Locale) */ protected Template getTemplate(Locale locale) throws IOException { String url = getUrl(); Assert.state(url != null, "'url' not set"); return getTemplate(url, locale); }
/** * Build a FreeMarker template model for the given model Map. * <p>The default implementation builds a {@link AllHttpScopesHashModel}. * @param model the model to use for rendering * @param request current HTTP request * @param response current servlet response * @return the FreeMarker template model, as a {@link SimpleHash} or subclass thereof */ protected SimpleHash buildTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) { AllHttpScopesHashModel fmModel = new AllHttpScopesHashModel(getObjectWrapper(), getServletContext(), request); fmModel.put(FreemarkerServlet.KEY_JSP_TAGLIBS, this.taglibFactory); fmModel.put(FreemarkerServlet.KEY_APPLICATION, this.servletContextHashModel); fmModel.put(FreemarkerServlet.KEY_SESSION, buildSessionModel(request, response)); fmModel.put(FreemarkerServlet.KEY_REQUEST, new HttpRequestHashModel(request, response, getObjectWrapper())); fmModel.put(FreemarkerServlet.KEY_REQUEST_PARAMETERS, new HttpRequestParametersHashModel(request)); fmModel.putAll(model); return fmModel; }
@Override protected void exposeHelpers(Map<String, Object> model, HttpServletRequest request) throws Exception { exposeAttribute(model, request); super.exposeHelpers(model, request); }
/** * Retrieve the FreeMarker template specified by the given name, * using the encoding specified by the "encoding" bean property. * <p>Can be called by subclasses to retrieve a specific template, * for example to render multiple templates into a single view. * @param name the file name of the desired template * @param locale the current locale * @return the FreeMarker template * @throws IOException if the template file could not be retrieved */ protected Template getTemplate(String name, Locale locale) throws IOException { return (getEncoding() != null ? getConfiguration().getTemplate(name, locale, getEncoding()) : getConfiguration().getTemplate(name, locale)); }