Resolving views in Spring MVC with Freemarker - including jsp page
When using the FreemarkerServlet, it is possible to include JSP pages alongside Freemarker content. However, I'm using Freemarker as a view resolver in my Spring MVC application and so don't use the FreemarkerServlet. I开发者_StackOverflows there any way I can still use @include_page in my Freemarker templates when I'm not using the FreemarkerServlet?
Thanks,
Andrew
I found the solution. You need to sub-class the FreemarkerView to add the include behaviour:
public class CustomFreeMarkerView extends FreeMarkerView
{
private TaglibFactory taglibFactory;
private ServletContextHashModel servletContextHashModel;
@Override
protected void doRender( Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response )
throws Exception
{
// Expose model to JSP tags (as request attributes).
exposeModelAsRequestAttributes( model, request );
// Expose all standard FreeMarker hash models.
model.put( FreemarkerServlet.KEY_JSP_TAGLIBS, this.taglibFactory );
model.put( FreemarkerServlet.KEY_APPLICATION,
this.servletContextHashModel );
model.put( FreemarkerServlet.KEY_SESSION, buildSessionModel( request,
response ) );
model.put( FreemarkerServlet.KEY_REQUEST, new HttpRequestHashModel(
request, response, getObjectWrapper() ) );
model.put( FreemarkerServlet.KEY_REQUEST_PARAMETERS,
new HttpRequestParametersHashModel( request ) );
model.put( FreemarkerServlet.KEY_INCLUDE, new IncludePage( request,
response ) );
if ( logger.isDebugEnabled() )
{
logger.debug( "Rendering FreeMarker template [" + getUrl()
+ "] in FreeMarkerView '" + getBeanName() + "'" );
}
// Grab the locale-specific version of the template.
Locale locale = RequestContextUtils.getLocale( request );
processTemplate( getTemplate( locale ), new SimpleHash( model ), response );
}
private HttpSessionHashModel buildSessionModel( HttpServletRequest request,
HttpServletResponse response )
{
HttpSession session = request.getSession( false );
if ( session != null )
{
return new HttpSessionHashModel( session, getObjectWrapper() );
}
else
{
return new HttpSessionHashModel( null, request, response,
getObjectWrapper() );
}
}
}
The key line:
model.put( FreemarkerServlet.KEY_INCLUDE, new IncludePage( request,
response ) );
and then sub-class the view resolver to utilise the custom view:
public class CustomFreeMarkerViewResolver extends FreeMarkerViewResolver
{
public CustomFreeMarkerViewResolver()
{
setViewClass( requiredViewClass() );
}
/**
* Requires {@link FreeMarkerView}.
*/
@Override
protected Class requiredViewClass()
{
return CustomFreeMarkerView.class;
}
}
This post was very helpful. I'm not sure if this will help, but here's my solution:
Here's my doRender method:
@Override
protected void doRender( Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response )
throws Exception
{
//Expose model to JSP tags (as request attributes).
exposeModelAsRequestAttributes(model, request);
// Expose all standard FreeMarker hash models.
SimpleHash fmModel = buildTemplateModel(model, request, response);
// add the include_page directive
fmModel.put( FreemarkerServlet.KEY_INCLUDE, new IncludePage( request, response));
if (logger.isDebugEnabled()) {
logger.debug("Rendering FreeMarker template [" + getUrl() + "] in FreeMarkerView '" + getBeanName() + "'");
}
// Grab the locale-specific version of the template.
Locale locale = RequestContextUtils.getLocale( request );
processTemplate(getTemplate(locale), fmModel, response);
}
drewzilla's answer is almost worked for me. but i had to use
getTemplate(locale).process(model, response.getWriter());
instead of
processTemplate( getTemplate( locale ), new SimpleHash( model ), response );
with spring
精彩评论