primefaces 实现保存主题,使主题应用于所有JSF页面

94 篇文章 0 订阅
85 篇文章 0 订阅
$(function() {
	//该el表达式失效
	//PrimeFaces.changeTheme('${sessionScope.theme == null?"swanky-purse":sessionScope.theme}');
	$.ajax({
		type:"post",
		url:"currentTheme.do",
		dataType:"text",
		success:function(data){
			
			PrimeFaces.changeTheme(data?data:"swanky-purse");
				
			console.log(data);
			
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			PrimeFaces.changeTheme('swanky-purse');
		}
	});
	
});


package com.xiuye.controller;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class ViewController {

	@RequestMapping("currentTheme.do")
	@ResponseBody
	public String theme(HttpSession session){
		String theme = (String) session.getAttribute("theme");
		if(theme == null){
			theme = "swanky-purse";
			session.setAttribute("theme", theme);
		}
		return theme;
	}
	
}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:p="http://primefaces.org/ui">


<h:head>
	<title>用户个人主页</title>
	<h:outputScript  library="js" name="current_theme.js">
		
	</h:outputScript>
	<h:outputStylesheet>
		
		.textInCenter{
			vertical-align:top;
			text-align:center;
			margin:auto;
		}
		
	</h:outputStylesheet>

</h:head>

<h:body>



	<h:form>

		<p:layout fullPage="true">


			<p:layoutUnit position="west" resizable="true" size="300">





			</p:layoutUnit>
			<p:layoutUnit position="north" resizable="true">
				<h:panelGrid columns="1">
					<p:outputLabel value="欢迎您,尊敬的用户:" title="dfa"></p:outputLabel>
				</h:panelGrid>
				<h:panelGrid width="100%" columns="3">
					<h:panelGrid columns="2">
						<p:outputLabel value="用户ID: "></p:outputLabel>
						<p:outputLabel value="${sessionScope.user.userid}"></p:outputLabel>
					</h:panelGrid>
					<h:panelGrid columns="2">
						<p:outputLabel value="用户名: "></p:outputLabel>
						<p:outputLabel value="${sessionScope.user.username}"></p:outputLabel>
					</h:panelGrid>
					<h:panelGrid columns="2">
						<p:outputLabel value="性别:"></p:outputLabel>
						<p:outputLabel value="${sessionScope.user.sex}"></p:outputLabel>
					</h:panelGrid>
					<h:panelGrid columns="2">
						<p:outputLabel value="生日: "></p:outputLabel>
						<p:outputLabel value="${sessionScope.user.birthday}">
							<f:convertDateTime pattern="yyyy年MM月dd日" />
						</p:outputLabel>
					</h:panelGrid>
					<h:panelGrid columns="2">
						<p:outputLabel value="Email: "></p:outputLabel>
						<p:outputLabel value="${sessionScope.user.email}"></p:outputLabel>
					</h:panelGrid>
					<h:panelGrid columns="2">
						<p:outputLabel value="QQ: "></p:outputLabel>
						<p:outputLabel value="${sessionScope.user.qq}"></p:outputLabel>
					</h:panelGrid>
					<h:panelGrid columns="2">
						<p:outputLabel value="手机号: "></p:outputLabel>
						<p:outputLabel value="${sessionScope.user.phone}"></p:outputLabel>
					</h:panelGrid>
					<h:panelGrid columns="2" rendered="${sessionScope.user.isAdmin}">
						<p:outputLabel value="管理员:"></p:outputLabel>
						<p:outputLabel value="${sessionScope.user.isAdmin?'是':'否'}"></p:outputLabel>
					</h:panelGrid>

				</h:panelGrid>
			</p:layoutUnit>
			<p:layoutUnit position="south" resizable="true">
				<h:panelGrid styleClass="textInCenter" columns="1">
					<p:outputLabel value="Xiuye Company Copyright © 2016 ">
					</p:outputLabel>
					<p:outputLabel value="All rights reserved."></p:outputLabel>
				</h:panelGrid>
			</p:layoutUnit>

			<p:layoutUnit position="center">

			</p:layoutUnit>
		</p:layout>
	</h:form>
</h:body>

</html>



关键部分原理:
1.JSF加载js等资源的路径要求是resources文件下的资源文件。
2.在浏览器端调用PrimeFaces.changeTheme方法可以runtime的时候,改变主题。
3.主题用ajax传到前台,调用2中的方法,用jquery的起始加载直接改变当前页面主题。








PrimeFaces主要标签学习。 1 PrimeFaces综述 3 1.1 安装 3 1.2 配置,JSF2.0环境下用PrimeFace2.x 4 1.3 Hello World入门示例 4 1.4 UI组件: 4 2 UI组件 5 2.1 布局 5 2.1.1 Layout 页面布局 5 2.1.2 Panel用于包含其它组件,提供象windows窗口式的外观。 8 2.1.3 TabView 分页式面板组件 8 2.1.4 OutputPanel 仅用于显示元素 9 2.1.5 Fieldset 9 2.1.6 Dashboard 仪表盘 10 2.1.7 Themeswitcher 主题切换器,动态切换主题 11 2.1.8 Separator空白分隔区域 11 2.1.9 Spacer行内加空格 11 2.2 菜单 11 2.2.1 Menu 11 2.2.2 Menubar 12 2.2.3 MenuButton 13 2.2.4 Toolbar 13 2.2.5 Stack :堆叠式菜单(竖向) 13 2.2.6 Dock :动画鱼眼式菜单(横向) 14 2.3 按钮: 15 2.3.1 Button 15 2.3.2 CommandButton 15 2.3.3 CommandLink 17 2.3.4 ContextMenu 17 2.3.5 HotKey 17 2.4 输入组件 18 2.4.1 文本输入 18 2.4.1.1 Editor 18 2.4.1.2 Password 19 2.4.1.3 Password Strength 19 2.4.1.4 inputMask 输入掩码,实现格式化输入。 19 2.4.1.5 InputText 20 2.4.1.6 InputTextarea 20 2.4.1.7 Watermark :文本输入内容提示 20 2.4.1.8 Keyboard 显示一个虚拟键盘,用以支持输入字符。 21 2.4.1.9 Inplace 替换文本 22 2.4.2 选择式输入 22 2.4.2.1 AutoComplete :自动补全 22 2.4.2.2 PickList 选择列表 25 2.4.2.3 Slider 滑动条 26 2.4.2.4 Spinner 27 2.4.3 其它格式数据的输入: 27 2.4.3.1 Spreadsheet电子表格 27 2.4.3.2 Calendar 各种格式的日期输入与显示 28 2.4.3.3 Schedule 日程计划输入组件 31 2.4.3.4 Captcha :变形字符验证 31 2.4.3.5 Color Picker 32 2.5 集合(复杂格式)数据的输出与显示: 33 2.5.1 BreadCrumb :层次化页面导航条 >…>….> 33 2.5.2 Accordion:一个容器组件,它用tab动态地显示折叠或展开过程。 34 2.5.3 Carousel:多用途,标签式、分布式显示 35 2.5.4 Galleria 图片陈列廊 36 2.5.5 LightBox :图片加亮显示 37 2.5.6 DataGrid 数据栅格 37 2.5.7 DataList 用列表的形式显示数据,每个栅格可显示多个数据项 39 2.5.8 DataTable数据表格 41 2.5.9 Tree 树形显示 46 2.5.10 TreeTable 树表 47 2.5.11 DragDrop 50 2.5.11.1 Draggable组件: 50 2.5.11.2 Droppable组件 51 2.5.12 Charts基于flash的图形生成与显示 52 2.6 数据导出: 54 2.6.1 Data Exporter 54 2.6.2 Printer 56 2.7 状态: 56 2.7.1 ProgressBar 56 2.7.2 NotificationBar 57 2.8 对话框: 58 2.8.1 ConfirmDialog 58 2.8.2 Dialog 58 2.9 图形图像多媒体: 59 2.9.1 ImageCompare :提供丰富的接口比较两副图像 59 2.9.2 Graphic Text 文本图象化显示 60 2.9.3 ImageCropper 60 2.9.4 ImageSwitch 61 2.9.5 Google Maps 地图 61 2.9.6 Dyna Image 63 2.9.7 Media 65 2.9.8 Star Rating 65 2.9.9 Wizard: 66 2.10 消息: 66 2.10.1 Growl Mac风格的消息显示 66 2.10.2 Message/Messages 67 2.10.3 Tooltip 67 2.11 文件处理: 67 2.11.1 FileUpload 上传文件 67 2.11.2 FileDownload 下载文件 69 2.11.3 IdleMonitor 屏幕凝滞 70 2.11.4 Terminal 70 2.12 辅助功能(辅助其它JSF组件,给它们添加新的功能和行为): 71 2.12.1 Ajax Engine 71 2.12.2 Ajax Poll轮询 72 2.12.3 Ajax远程调用p:remoteCommand 72 2.12.4 Ajax Status 显示ajax后台运行状态。 72 2.12.5 Focus 73 2.12.6 Effect: 73 2.12.7 Collector : 74 2.12.8 Resizable 给任何JSF组件添加可调整大小的行为。 74 2.12.9 RequestContext : 75 3 TouchFaces 76 3.1.1 移动UI工具 76 3.1.2 Ajax Push/Comet 77 3.1.3 几分钟实现聊天用: 78 4 附录 79 4.1 全部UI组件列表 84 4.2 PrimeFaces常用属性集 85
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值