ZK 数据传输一:使用注解注入数据(jsp页面形式)

建立页面index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="cn.wempire.zkcrud.web.ui.MyValue"%>
<%@ page import="cn.wempire.zkcrud.pojo.*"%>
<%@ taglib uri="http://www.zkoss.org/jsp/zul" prefix="z"%>

<z:init use="org.zkoss.zkplus.databind.AnnotateDataBinderInit" />

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ZK CRUD Demo</title>
</head>
<body>
<h1>ZK CRUD Demo</h1>
<%
	//JSP scriplet to generate test data "my_list", can be easily wrapped to normal JSP tags...
	final ArrayList my_list = new ArrayList();
	final SimpleDateFormat format = new SimpleDateFormat(
			"yyyy/MM/dd hh:mm:ss");
	for (int i = 0; i < 10; i++)
	{
		final String ref = "标识index: " + i;
		my_list.add(new MyValue()
		{
			public String getDate()
			{
				return format.format(new Date());
			}

			public String getValue()
			{
				return ref;
			}
		});
	}

	request.setAttribute("my_list", my_list);// use Request to store and pass data.
%>

<z:page zscriptLanguage="java">
	<%-- this is JSP valid Comment, do not use XML comment. --%>

	<z:window id="win1" apply="cn.wempire.zkcrud.web.ui.MyComposer"
		use="cn.wempire.zkcrud.web.ui.MyWindow" width="550px" border="normal">
		<z:button label="OK" forward="onOK1" />
		<z:button label="Cancel" forward="onCancel1" />
	</z:window>
	<z:separator />
	<z:separator />

	<z:zscript>
		System.out.println("当页面加载时启动..." );
		ArrayList my_list2 = new ArrayList();
		my_list2 = requestScope.get("my_list");// get data from request and declare page variable "my_list" implicitly.
		
	</z:zscript>

	<z:window title="JSP方式的注入数据---------->Values" id="win2" width="550px">
		<%-- same as original ZK databinding...--%>
		<z:listbox id="feedListBox" model="@{my_list2}" rows="2"
			selectedItem="@{selected}">
			<z:listitem self="@{each='my'}">
				<z:listcell label="@{my.value}" />
				<z:listcell label="@{my.date}" />
			</z:listitem>
		</z:listbox>


	</z:window>
	
</z:page>
</body>
</html>

 @{}注入数据时使用的表达式,记得要加上<z:init use="org.zkoss.zkplus.databind.AnnotateDataBinderInit" />才可使用注释绑定数据(*.zul的话使用<?init        class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>)。

  MyValue是一个简单的实体类。

 

 

  上面的<z:window/>是用于触发事件的,apply和use的用法就不介绍了,如果想用MVC的方式在后台产生或操作组  件,那么这两个标签属性就得熟悉怎么用了。使用到下面两个类。

   MyComposer.java

 

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.Composer;
import org.zkoss.zul.Window;

public class MyComposer implements Composer
{


	public void doAfterCompose(Component comp)
	{
		Window window = (Window) comp;
		window.setTitle("用Composer初始化的窗口"); // do whatever initialization you
		// want
		// comp is Window since we will specify it to a window later
		// window.add
	}

}

 

MyWindow.java

 

public class MyWindow extends Window
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void onOK1() throws Exception
	{
		System.out.println("you are pressing on OK1");
	}

	public void onCancel1() throws Exception
	{
		System.out.println("you are pressing on CANCEL1");
	}

}

 

 

你可能感兴趣的:(jsp,UI,Web,mvc,zk)