【问题标题】:Apache Tiles : change template page at runtimeApache Tiles:在运行时更改模板页面
【发布时间】:2010-10-21 22:08:15
【问题描述】:

我有一个问题:假设在 Spring MVC 3.0 环境中我使用 Tiles 管理视图:我有一个包含所有视图定义的 xml 文件。每个视图都扩展了一个特定的模板。我有两个模板:一个用于渲染 completeDOM (),一个用于 partialDOM (.....)。问题是,有些视图可以在 fullDOM 和 partialDOM 中检索,但我不想写两个相似的定义。

我正在考虑一种动态方法:在运行时注入视图的模板,指定一个应该包含模板名称的 http 参数。如果请求中包含参数,则 Tiles 应该覆盖视图扩展的模板,模板由 http 参数值检测。

有什么建议吗?

【问题讨论】:

    标签: spring-mvc tiles


    【解决方案1】:

    我知道这是一个老问题,但我需要做这件事,所以我想我会分享我的解决方案。

    Tiles 允许它们称为“runtime composition”,它允许您修改定义。因此,您可以重用现有定义并交换模板:

    <tiles:insertDefinition name="existingDefinition" template="alternateTemplate.jsp" />
    

    【讨论】:

      【解决方案2】:

      在 spring tilesConfigurer 中你需要设置可变容器:

      <property name="useMutableTilesContainer" value="true"/>
      <property name="checkRefresh" value="true"/>
      

      在你的 Spring 控制器中:

      ModelAndView model = new ModelAndView();
      MutableTilesContainer container = (MutableTilesContainer)ServletUtil.getContainer(request.getSession().getServletContext());
      Attribute attribute = new Attribute("your template jsp");
      HashMap<String, Attribute>  attributes = new HashMap<String, Attribute>();
      attributes.put("body", attribute);
      Definition definition = new Definition("your definition name", "your jsp", attributes);
      definition.setExtends("your definition template name");
      definition = PatternUtil.replacePlaceholders(definition, "your definition name", new Object());
      container.register(definition, request, response);
      model.setViewName("your definition name");
      

      【讨论】:

        【解决方案3】:

        我认为视图准备器可能会有所帮助: http://tiles.apache.org/framework/tutorial/advanced/preparer.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-04
          • 2016-02-02
          • 2018-12-21
          相关资源
          最近更新 更多