7

我仍然不确定是否正确使用 JSF 模板和复合组件。我需要创建一个企业 Web 应用程序,它会有很多页面。每个页面都有相同的页眉、菜单、页脚,当然还有不同的内容(= JSF 模板)。每个页面上的内容都由可重复使用的“盒子”(= JSF 复合组件)组成。这些框由一些文件、按钮等组成。我的解决方案是否正确?或者我应该使用其他技术,如自定义组件、装饰......?

布局.xhtml

<h:body>
    <ui:insert name="main_menu">
        <ui:include src="/xhtml/template/main_menu.xhtml"/>
    </ui:insert>
    <ui:insert name="header">
        <ui:include src="/xhtml/template/header.xhtml"/>
    </ui:insert>
    <ui:insert name="content"/>
    <ui:insert name="footer">
        <ui:include src="/xhtml/template/footer.xhtml"/>
    </ui:insert>
</h:body>

customer_overview.xhtml:

<html xmlns:cc="http://java.sun.com/jsf/composite/composite_component">
<h:body>
    <!-- Facelet template -->
    <ui:composition template="/xhtml/template/layout.xhtml">
        <ui:define name="content">
            <!-- Composite Components -->
            <cc:component_case_history
                caseList="#{customerOverviewController.cases}"
            />
            <cc:component_customer
                ....
            />
            ...
        </ui:define>
    </ui:composition>
</h:body>

component_case_history.xhtml

<html xmlns:composite="http://java.sun.com/jsf/composite">
<composite:interface>
    <composite:attribute name="cases" type="java.util.List"/>
</composite:interface>

<composite:implementation>
    <!-- using of "cases" -->
    ...
</composite:implementation>

CustomerOverviewController.java

@ManagedBean
@ViewScoped
public class CustomerOverviewController {
    public List<Case> getCases() {
        ...
    }
}

编辑 2012-04-27

基于: 何时使用 <ui:include>、标记文件、复合组件和/或自定义组件?

我认为我应该使用 Facelet 模板 + Facelet 标记文件,而不是 Facelet 模板 + 复合组件。

4

1 回答 1

6

布局、模板

布局.xhtml:

每个页面都有相同的页眉、菜单、页脚......

在这种情况下,您可以省略页眉、菜单、页脚的 ui:insert 标记。

<h:body>
    <ui:include src="/xhtml/template/main_menu.xhtml"/>
    <ui:include src="/xhtml/template/header.xhtml"/>
    <ui:insert name="content"/>
    <ui:include src="/xhtml/template/footer.xhtml"/>
</h:body>

你也可能有一个没有名字的 ui:insert,所以如果你想进一步简化:

<h:body>
    <ui:include src="/xhtml/template/main_menu.xhtml"/>
    <ui:include src="/xhtml/template/header.xhtml"/>
    <ui:insert/>
    <ui:include src="/xhtml/template/footer.xhtml"/>
</h:body>

customer_overview.xhtml:

如果您在 layout.xhtml 中有没有名称的 ui:insert,则此处不需要 ui:define:

<ui:composition template="/xhtml/template/layout.xhtml">
        <!-- Composite Components -->
        <cc:component_customer/>
        <cc:component_case_history
            caseList="#{customerOverviewController.cases}"
        />
        ...
</ui:composition>

此外,您应该将模板放在用户无法直接访问的文件夹中 (WEB-INF)。

可重复使用的“盒子”

您的复合组件之一如下所示:

<cc:component_customer/>

没有任何属性的组件是非常可疑的。

  • 它有什么作用?
  • 显示用户名?
  • 如果您不传递任何属性,它如何获取用户名?

一个组件应该是独立的,对于其他可重复使用的部分,请使用 ui:insert 代替。

于 2012-04-08T10:42:52.893 回答