福网

ZK AJAX 学习笔记第十三天 使用ZK记录历史路径

ZK AJAX 学习笔记

—-Run Java and JSP on ZK

AJAX有一个缺点就是不能记录浏览路径。今天在翻译ZK开发手册第十章高级特性一章时,发现了ZK对此的解决方法,这样,也可使使用浏览器的”后退”或”前进”按钮。下面以ZK Tree为例来说明,此例以为9. 第8天 ZK 的 Tree基础。

页面文件index.jsp

<%@ page language="java" pageEncoding="UTF-8"%>

<html xmlns:x="http://www.zkoss.org/2005/zul">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>ZK 记录历史路径</title>
</head>
<body>
<?page id="history"?>
<x:hbox>
	<x:tree id="toc" use="edu.bigc.zk.history.HistoryTree" vflex="true"
		width="200px">
		<x:treecols>
			<x:treecol image="/resource/images/gohome.png" label="欢迎" />
		</x:treecols>
		<x:treechildren>
			<x:treeitem id="s1" label="欢迎" image="/resource/images/wizard.png"
				value="/tree/welcome.jsp" />
			<x:treeitem id="s2" label="你喜欢zk吗?" image="/resource/images/help.png"
				value="/tree/likezk.jsp">
				<x:treechildren>
					<x:treeitem id="s3" label="当然喜欢!" image="/resource/images/tux.png"
						value="/tree/lovezk.jsp" />
				</x:treechildren>
			</x:treeitem>
		</x:treechildren>
	</x:tree>
	<x:window id="contents" border="normal" width="99%">
		<x:include id="xcontents" src="${requestScope.contentSrc}" />
	</x:window>
	<x:label visible="false" use="edu.bigc.zk.history.HistoryLabel" />
</x:hbox>

</body>
</html>

welcome.jsp

<%@ page language="java" pageEncoding="UTF-8"%>

<html xmlns:x="http://www.zkoss.org/2005/zul">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>欢迎</title>
</head>
<body>
欢迎
</body>
</html>

 

likezk.jsp

<%@ page language="java" pageEncoding="UTF-8"%>

<html xmlns:x="http://www.zkoss.org/2005/zul">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>喜欢zk吗?</title>
</head>
<body>
喜欢zk吗?
</body>
</html>

lovezk.jsp

<%@ page language="java" pageEncoding="UTF-8"%>

<html xmlns:x="http://www.zkoss.org/2005/zul">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>当然喜欢</title>
</head>
<body>
当然喜欢
</body>
</html>

HistoryTree.java

package edu.bigc.zk.history;

import org.zkoss.zk.ui.ComponentNotFoundException;
import org.zkoss.zk.ui.Path;
import org.zkoss.zk.ui.UiException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Include;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Treeitem;

public class HistoryTree extends Tree {

	private static final long serialVersionUID = 1L;

	public void onCreate() {
		this.getPage().addEventListener("onBookmarkChanged",
				new EventListener() {
					public void onEvent(Event event) throws UiException,
							InterruptedException {
						try {
							((Treeitem) getPage().getFellow(
									getPage().getDesktop().getBookmark()))
									.setSelected(true);
							onSelect();
						} catch (ComponentNotFoundException ex) {
							((Treeitem) getPage().getFellow("s1"))
									.setSelected(true);
							onSelect();
						}
					}
				});

	}

	public void onSelect() {
		Treeitem item = getSelectedItem();
		this.getDesktop().setBookmark(item.getId());
		if (item != null) {
			Include inc = (Include) Path.getComponent("/contents/xcontents");
			inc.setSrc((String) item.getValue());
		}
	}
}

HistoryLabel.java

package edu.bigc.zk.history;

import org.zkoss.zk.ui.Path;
import org.zkoss.zk.ui.ext.AfterCompose;
import org.zkoss.zul.Include;
import org.zkoss.zul.Label;
import org.zkoss.zul.Treeitem;

public class HistoryLabel extends Label implements AfterCompose {

	private static final long serialVersionUID = 1L;

	public void afterCompose() {
		Treeitem item = (Treeitem) getSpaceOwner().getFellow("s1");
		Include showInc = (Include) Path.getComponent("/contents/xcontents");
		showInc.setSrc((String) item.getValue());
	}
}

运行结果(注意路径的变化)

Comments are currently closed.