使用Freemarker解析Spring MVC中的视图 - 包括jsp页面

时间:2010-05-26 13:53:47

标签: spring spring-mvc freemarker

使用FreemarkerServlet时,可以在Freemarker内容中包含JSP页面。但是,我在我的Spring MVC应用程序中使用Freemarker作为视图解析器,所以不要使用FreemarkerServlet。当我没有使用FreemarkerServlet时,有什么方法可以在我的Freemarker模板中使用@include_page吗?

谢谢,

安德鲁

3 个答案:

答案 0 :(得分:2)

这篇文章非常有帮助。我不确定这是否会有所帮助,但这是我的解决方案:

这是我的doRender方法:

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

答案 1 :(得分:1)

我找到了解决方案。您需要对FreemarkerView进行子类化以添加包含行为:

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

}

关键线:

  model.put( FreemarkerServlet.KEY_INCLUDE, new IncludePage( request,
        response ) );

然后对视图解析器进行子类化以使用自定义视图:

public class CustomFreeMarkerViewResolver extends FreeMarkerViewResolver
{

   public CustomFreeMarkerViewResolver()
   {
      setViewClass( requiredViewClass() );
   }

   /**
    * Requires {@link FreeMarkerView}.
    */
   @Override
   protected Class requiredViewClass()
   {
      return CustomFreeMarkerView.class;
   }
}

答案 2 :(得分:0)

drewzilla的回答几乎对我有用。但我不得不使用 getTemplate(locale).process(model, response.getWriter());
代替 processTemplate( getTemplate( locale ), new SimpleHash( model ), response ); 春天