我仍然不确定如何正确使用 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>
客户概览.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
基于: When to use <ui:include>, tag files, composite components and/or custom components?
我认为我应该使用 Facelet 模板 + Facelet 标记文件而不是 Facelet 模板 + 复合组件。
最佳答案
布局、模板
layout.xhtml:
Every page will have the same header, menu, footer ...
在这种情况下,您可以省略页眉、菜单、页脚的 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>
客户概览.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。
关于java - 正确使用 Facelet 模板和复合组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10056008/