views:

275

answers:

4

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. Is there any way I can still use @include_page in my Freemarker templates when I'm not using the FreemarkerServlet?

Thanks,

Andrew

A: 

Hi Andrew,

Did you find a way to do this? I am trying to do the same thing but haven't found any references on how I can.

Deepa
See my solution below...
drewzilla
A: 

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;
   }
}
drewzilla
A: 

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

Manoj
A: 

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);
   }