JSF 2.2: Theme

#JSF 2.2: Theme Support#

Richfaces and Primefaces have theme support, it is an attractive feature of the skin-able JSF components projects.

JSF 2.2 introduces a new feature named Resource Library Contracts which supports to apply different resources(css and js) and facelets template at runtime.

contracts resources files can be placed in /contracts folder under the web application root or /META-INF/contracts on classpath.

An example of contracts resources file structure in a web application.

<pre> /contracts /default /css defautl.css cssLayout.css template.xhtml /alternative /css defautl.css cssLayout.css template.xhtml </pre>

There are two contracts defined in the project, default and alternative.

The following is the content of /contracts/default/template.xhtml.

<pre> &lt;?xml version='1.0' encoding='UTF-8' ?> &lt;!DOCTYPE html> &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:h="http://xmlns.jcp.org/jsf/html"> &lt;h:head> &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> &lt;link href="#{request.contextPath}/contracts/default/css/default.css" rel="stylesheet" type="text/css" /> &lt;link href="#{request.contextPath}/contracts/default/css/cssLayout.css" rel="stylesheet" type="text/css" /> &lt;title>Facelets Template&lt;/title> &lt;/h:head> &lt;h:body> &lt;div id="top" class="top"> &lt;ui:insert name="top">Top&lt;/ui:insert> &lt;/div> &lt;div id="content" class="center_content"> &lt;ui:insert name="content">Content&lt;/ui:insert> &lt;/div> &lt;/h:body> &lt;/html> </pre>

The content of /contracts/alternative/template.xhtml is similar, only the css reference path is changed.

<pre> &lt;?xml version='1.0' encoding='UTF-8' ?> &lt;!DOCTYPE html> &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:h="http://xmlns.jcp.org/jsf/html"> &lt;h:head> &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> &lt;link href="#{request.contextPath}/contracts/alternative/css/default.css" rel="stylesheet" type="text/css" /> &lt;link href="#{request.contextPath}/contracts/alternative/css/cssLayout.css" rel="stylesheet" type="text/css" /> &lt;title>Facelets Template&lt;/title> &lt;/h:head> &lt;h:body> &lt;div id="top" class="top"> &lt;ui:insert name="top">Top&lt;/ui:insert> &lt;/div> &lt;div id="content" class="center_content"> &lt;ui:insert name="content">Content&lt;/ui:insert> &lt;/div> &lt;/h:body> &lt;/html> </pre>

In your facelets template file, you can use the contracts template directly. NOTE: The template path is /template.xhtml.

<pre> &lt;ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"> &lt;f:view contracts="alternative"> &lt;ui:composition template="/template.xhtml" &lt;ui:define name="content"> &lt;h1>Theme Switcher&lt;/h1> &lt;p>Sample of applying alternative.&lt;/p> &lt;/ui:define> &lt;/ui:composition> &lt;/f:view> &lt;/ui:composition> </pre>

In JSF 2.2, a new attribute contracts was added to f:view which can specify the specified contracts name, default and alternative in this project.

You can switch to the contracts dynamically through a backend bean.

<pre> &lt;ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"> &lt;f:view contracts="#{themeSwitcher.theme}"> &lt;ui:composition template="/template.xhtml"> &lt;ui:define name="content"> &lt;h1>Theme Switcher&lt;/h1> &lt;p>Current Theme:#{themeSwitcher.theme}&lt;/p> &lt;h:form> &lt;h:commandButton value="Default Theme" action="#{themeSwitcher.changeTheme('default')}"> &lt;/h:commandButton> &lt;h:commandButton value="Alternative Theme" action="#{themeSwitcher.changeTheme('alternative')}"> &lt;/h:commandButton> &lt;/h:form> &lt;/ui:define> &lt;/ui:composition> &lt;/f:view> &lt;/ui:composition> </pre>

In the template file, use two h:commandButton buttons to switch the contracts dynamically.

<pre> @Named @SessionScoped public class ThemeSwitcher implements Serializable { @Inject transient Instance&lt;Logger> log; private String theme = "default"; public void changeTheme( String _theme){ log.get().log(Level.INFO, "call changeTheme{0}", _theme); switch (_theme) { case "alternative": this.theme = "alternative"; break; default: this.theme = "default"; break; } } public String getTheme() { return theme; } public void setTheme(String theme) { this.theme = theme; } } </pre>

You can also configure the contracts in faces-config.xml file.

<pre> &lt;application> &lt;resource-library-contracts> &lt;contract-mapping> &lt;url-pattern>/themed-alt/*&lt;/url-pattern> &lt;contracts>alternative&lt;/contracts> &lt;/contract-mapping> &lt;contract-mapping> &lt;url-pattern>/themed-default/*&lt;/url-pattern> &lt;contracts>default&lt;/contracts> &lt;/contract-mapping> &lt;/resource-library-contracts> &lt;/application> </pre>

The /themed-alt will use the alternative and /themed-default will use default contracts.

Unfortunately, JSF 2.2 does not support EL as the value of contracts in faces-config.xml.

<pre> &lt;contract-mapping> &lt;url-pattern>/themed-dyn/*&lt;/url-pattern> &lt;contracts>#{themeSwitcher.theme}&lt;/contracts> &lt;/contract-mapping> </pre>

This could be supported in a future version.

NOTE: In order to demonstrate the usage of Resources Library Contracts, I only changed the background color style in the css file. In the real world application, it could be much difference. For example, you use different css and layout for before and after login case.

Check out the complete codes from my github.com, and play it yourself.

https://github.com/hantsy/ee7-sandbox

转载于:https://my.oschina.net/hantsy/blog/145980

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值