【问题标题】:Dynamically composing pages using tiles使用磁贴动态组合页面
【发布时间】:2011-06-16 11:54:47
【问题描述】:

我是一个新的瓷砖用户,到目前为止我很喜欢它。但我现在面临一个问题,我不确定瓷砖是否是我实现目标的正确工具。

我想做的是创建一个通用注册表单,在多个站点之间共享。 此注册表在每个站点上会略有不同。很明显,他们可以使用相同的 jsp 文件和 java 代码。我的目标是拥有一个 .properties 文件,我可以在其中配置哪些输入表单片段应该包含在表单中,哪些不应该包含在表单中(对于每个站点)。

有没有办法动态插入属性?

我创建了一个视图准备器类。这样我可以添加/覆盖现有属性。

public class TestViewPreparer extends ViewPreparerSupport {
    public void execute(TilesRequestContext tilesContext, AttributeContext attributeContext) throws PreparerException {
        Attribute attribute = new Attribute("/WEB-INF/views/search-panel/holiday-type.jsp");
        attributeContext.putAttribute("rfFragment", attribute);
    }
}

但这一次只添加/覆盖一个属性。除了在模板文件中,我还必须为它们中的每一个添加以下行。

<tiles:insertAttribute name="rfFragment1" />
<tiles:insertAttribute name="rfFragment2" />
    //... etc

我正在寻找的更像是添加Attributes 的列表,并通过循环将它们包含在模板行中。有点像这样:

    <tiles:useAttribute id="fragments" name="rfFragments" classname="java.util.List" />
    <c:forEach var="fragment" items="${fragments}">
        <tiles:insertAttribute value="${fragment}" flush="true" /></c:forEach>

我的问题是AttributeContext 只能采用一个属性,而不能是属性列表。还是我错过了什么?

我希望我已经足够详细地描述了我的问题。

【问题讨论】:

    标签: java tiles apache-tiles


    【解决方案1】:

    我目前正在处理类似的问题,尝试使用 ViewPreparer 在磁贴中创建动态导航菜单。根据下面链接的帖子,您可以通过 TilesRequestContext 而不是使用 AttributeContext 传递 ArrayLists。我自己还没有完全让它工作,但也许这些链接会帮助将来寻找这个的人(让我永远找到好的例子)

    ViewPreparer example

    Injecting the controller with Spring

    我的实现:

    public class HeaderController extends ViewPreparerSupport{
    private EvalTypesService evalTypesService;
    
    @Autowired
    public HeaderController(EvalTypesService evalTypesService){
        this.evalTypesService = evalTypesService;
    }
    
    @Override
    public void execute(TilesRequestContext tilesContext, AttributeContext attributeContext) throws PreparerException{
        List<EvalMasterEvaluationType> evalTypes = evalTypesService.getAllActiveEvalTypes();
        tilesContext.getRequestScope().put("evalTypes", evalTypes);
    }
    

    }

    view.xml 中的定义:

    <definition name=".mainTemplate" template="/WEB-INF/views/main_template.jsp" preparer="myPathToController.HeaderController">
        <put-attribute name="header" value="/WEB-INF/views/tiles/header.jsp" />
        <put-attribute name="content" value="" />
        <put-attribute name="footer" value="/WEB-INF/views/tiles/footer.jsp" />
    </definition>
    

    dispatch servlet xml 中的 Tiles Bean:

    <bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver"/>
    
    <bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/views/**/views.xml</value>
            </list>
        </property>
        <property name="preparerFactoryClass" value="org.springframework.web.servlet.view.tiles2.SimpleSpringPreparerFactory" /></bean>
    

    在 header.jsp 磁贴中:

    <div id="nav">
    <c:out value="${msg}"></c:out>
    <ul>
        <c:forEach var="type" items="${evalTypes}">
        <li><a href="<s:url value="/evalTypes/${type.id}" />"><c:out value="${type.name}" /></a></li>
    
        </c:forEach>
    </ul>
    

    【讨论】:

      猜你喜欢
      • 2012-02-29
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多