何时在 JSF 2.2 中使用资源库契约?
When to use Resource Library Contract in JSF 2.2?
我知道创建资源库合约是为了支持模板化,但即使没有它,也可以通过 template.xhtml 中的 ui:insert 轻松实现模板化:
<div id="content">
<ui:insert name="content" />
</div>
可以在 Web 应用程序根目录下使用 'templates' 目录,而不是创建 "contracts" 目录。
那么,使用合同有什么好处?
要创建多个模板并将它们应用于同一应用程序的不同页面,还允许用户select他们想要应用的模板。
Well, you know, both the flows and the resource library contracts were
obviously, big-ticket features. And a resource library contract is
really, really powerful if you're in a situation where you want to
support both mobile and a desktop client because you can just switch
back and forth between a set of templates that you just define. So
that is a really strong feature.
And hopefully, people see that. But the nice thing about it, it's a
stackable feature. So what that means is you could actually have a
resource library contract for a layout, and then a resource library
contract on top of that for color styling or something like that. So
you could switch back and forth between -- I'm just using an example,
obviously -- a blue and a red styling, and a mobile versus desktop
styling. So that would be four permutations that you would already
have.
That's the other thing that jumps out at me because I've -- I'm sure
you ran into this as well. I had clients who basically have a product
that they sell, and either it's a hosted product or it's an on premise
product. But either way, each client wants a different look and feel,
and different sets of parameters, and things like that. And you can
do a lot of that in the database, but at some point, you want to
actually change the templates, and the resource library contracts
makes that very easy to use. So I think that's one of my favorite
features, too.
资源库合约允许您拥有多个资源库,这些资源库是在 JSF 2.0
中引入的,其中 css、js、图像和复合组件位于其中。
- 所有模板文件必须命名相同
- 全部
<ui:inserts name="must have the same attribute value" />
有几种方法可以应用它。
-Web Pages/
--contracts/
---resource-1/
-----template.xhtml
-----css/
-----js/
-----img/
---resource-2/
-----template.xhtml
-----css/
-----js/
-----img/
#1:通过 URL 模式自动将合同映射到视图:
在faces-config.xml文件中
<application>
<contract-mapping>
<url-pattern>*</url-pattern>
<contracts>resource-1</contracts>
</contract-mapping>
<contract-mapping>
<url-pattern>/admin/*</url-pattern>
<contracts>resource-2</contracts>
</contract-mapping>
</application>
#2 设置每个视图的合同:
这允许用户select使用设计(模板)。
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<f:view contracts="#{templateMB.template}">
<ui:composition template="/template.xhtml">
<ui:define name="content">
<h:form>
<h:selectOneMenu valueChangeListener="#{templateMB.tempEvent}"
onchange="submit()">
<f:selectItem itemLabel="..." itemValue="..."/>
<f:selectItem itemLabel="resource-1" itemValue="resource-1"/>
<f:selectItem itemLabel="resource-2" itemValue="resource-2"/>
</h:selectOneMenu>
</h:form>
</ui:define>
</ui:composition>
</f:view>
</html>
托管bean所在位置:
@ManagedBean
@SessionScoped
public class TemplateMB {
private String template = "resource-1"; // make default
public TemplateMB() {
}
public void tempEvent(ValueChangeEvent e){
template = (String) e.getNewValue();
}
public String getTemplate() {
return template;
}
}
我知道创建资源库合约是为了支持模板化,但即使没有它,也可以通过 template.xhtml 中的 ui:insert 轻松实现模板化:
<div id="content">
<ui:insert name="content" />
</div>
可以在 Web 应用程序根目录下使用 'templates' 目录,而不是创建 "contracts" 目录。
那么,使用合同有什么好处?
要创建多个模板并将它们应用于同一应用程序的不同页面,还允许用户select他们想要应用的模板。
Well, you know, both the flows and the resource library contracts were obviously, big-ticket features. And a resource library contract is really, really powerful if you're in a situation where you want to support both mobile and a desktop client because you can just switch back and forth between a set of templates that you just define. So that is a really strong feature.
And hopefully, people see that. But the nice thing about it, it's a stackable feature. So what that means is you could actually have a resource library contract for a layout, and then a resource library contract on top of that for color styling or something like that. So you could switch back and forth between -- I'm just using an example, obviously -- a blue and a red styling, and a mobile versus desktop styling. So that would be four permutations that you would already have.
That's the other thing that jumps out at me because I've -- I'm sure you ran into this as well. I had clients who basically have a product that they sell, and either it's a hosted product or it's an on premise product. But either way, each client wants a different look and feel, and different sets of parameters, and things like that. And you can do a lot of that in the database, but at some point, you want to actually change the templates, and the resource library contracts makes that very easy to use. So I think that's one of my favorite features, too.
资源库合约允许您拥有多个资源库,这些资源库是在 JSF 2.0
中引入的,其中 css、js、图像和复合组件位于其中。
- 所有模板文件必须命名相同
- 全部
<ui:inserts name="must have the same attribute value" />
有几种方法可以应用它。
-Web Pages/
--contracts/
---resource-1/
-----template.xhtml
-----css/
-----js/
-----img/
---resource-2/
-----template.xhtml
-----css/
-----js/
-----img/
#1:通过 URL 模式自动将合同映射到视图:
在faces-config.xml文件中
<application>
<contract-mapping>
<url-pattern>*</url-pattern>
<contracts>resource-1</contracts>
</contract-mapping>
<contract-mapping>
<url-pattern>/admin/*</url-pattern>
<contracts>resource-2</contracts>
</contract-mapping>
</application>
#2 设置每个视图的合同: 这允许用户select使用设计(模板)。
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<f:view contracts="#{templateMB.template}">
<ui:composition template="/template.xhtml">
<ui:define name="content">
<h:form>
<h:selectOneMenu valueChangeListener="#{templateMB.tempEvent}"
onchange="submit()">
<f:selectItem itemLabel="..." itemValue="..."/>
<f:selectItem itemLabel="resource-1" itemValue="resource-1"/>
<f:selectItem itemLabel="resource-2" itemValue="resource-2"/>
</h:selectOneMenu>
</h:form>
</ui:define>
</ui:composition>
</f:view>
</html>
托管bean所在位置:
@ManagedBean
@SessionScoped
public class TemplateMB {
private String template = "resource-1"; // make default
public TemplateMB() {
}
public void tempEvent(ValueChangeEvent e){
template = (String) e.getNewValue();
}
public String getTemplate() {
return template;
}
}