ZK 数据传输二:使用Hibernate/Spring在后台获取数据(jsp页面,实现CRUD)

index.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.zkoss.org/jsp/zul" prefix="z"%>

<!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 Data Demo</title>
</head>
<body>
<z:page zscriptLanguage="java">

<z:window id="win3" title="Spring后台获取数据---------->Users"
		border="normal" width="500px"
		use="cn.wempire.zkcrud.web.ui.UserWindow">
		<z:vbox>
			<z:listbox  mold="paging" id="lb" width="490px" rows="5" pageSize="5">
				<z:listhead>
					<z:listheader label="LoginName" width="150px" sort="auto" />
					<z:listheader label="Password" width="150px"  sort="auto"/>
				</z:listhead>
			</z:listbox>
			<z:hbox>
				<z:button label="Change Paging Mold">
					<z:attribute name="onClick">
						lb.pagingChild.mold = "os".equals(lb.pagingChild.mold) ? "default" : "os";
					</z:attribute>
				</z:button>
				<z:button label="创建" forward="onNew" />
				<z:button label="更新" forward="onUpdate" />
				<z:button label="删除" forward="onDelete" />
			</z:hbox>

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

 分页,排序(使用默认算法)都在下面一段代码中,一看就懂,不赘述:

   <z:listbox  mold="paging" id="lb" width="490px" rows="5" pageSize="5">
    <z:listhead>
     <z:listheader label="LoginName" width="150px" sort="auto" />
     <z:listheader label="Password" width="150px"  sort="auto"/>
    </z:listhead>
   </z:listbox>


在后台创建Window的类UserWindow:

public class UserWindow extends Window
{

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

	private UserService userService;
	private List users;

	public void onCreate() throws Exception
	{
		ApplicationContext ctx = WebApplicationContextUtils
				.getRequiredWebApplicationContext((ServletContext) getDesktop()
						.getWebApp().getNativeContext());
		userService = (UserService) ctx.getBean("userService");

		users = userService.findAllUsers();
		render();
	}

	protected void render()
	{
		Listbox lb = (Listbox) this.getFellow("lb");
		lb.getItems().clear();

		for (Iterator it = users.iterator(); it.hasNext();)
		{
			User t = (User) it.next();

			Listitem lt = new Listitem();
			lt.setValue(t);
			Listcell lc = new Listcell(t.getLoginName());
			lc.setHeight("30px");
			lt.appendChild(lc);
			Listcell lc2 = new Listcell(t.getPassword());
			lc2.setHeight("30px");
			lt.appendChild(lc2);

			lb.appendChild(lt);
		}
	}

	public void onNew() throws Exception
	{
		Window win = (Window) Executions.createComponents("task.zul", null,
				null);
		win.doModal();
		if (win.getAttribute("OK") != null)
		{
			users = userService.findAllUsers();
			render();
		}
	}

	public void onUpdate() throws Exception
	{
		Listbox lb = (Listbox) this.getFellow("lb");
		Listitem lt = lb.getSelectedItem();
		if (lt == null)
			return;
		User t = (User) lt.getValue();

		Map params = new HashMap();
		params.put("user", t);
		Window win = (Window) Executions.createComponents("task.zul", null,
				params);
		win.doModal();
		if (win.getAttribute("OK") != null)
		{
			users = userService.findAllUsers();
			render();
		}
	}

	public void onDelete() throws Exception
	{
		Listbox lb = (Listbox) this.getFellow("lb");
		Listitem lt = lb.getSelectedItem();
		if (lt == null)
			return;
		User t = (User) lt.getValue();
		userService.delete(t.getId());
		lt.detach();
	}

}

 

用于创建和更新数据的页面task.zul,注意user属性与后台的对应,arg指后台传递过来的数据(即UserWindow中的params):

 

<?xml version="1.0" encoding="utf-8"?>
<window id="userWnd" title="User" border="normal" width="300px" 
  use="cn.wempire.zkcrud.web.ui.PopWindow" user="${arg.user}">
	<vbox>
		<grid>
		<columns>
			<column width="150px"/>
		</columns>
		<rows>
			<row>
				LoginName: 
				<textbox id="LoginName"/>
			</row>
			<row>
				Password: 
				<textbox id="Password" rows="5" cols="25"/>
			</row>
		</rows>
		</grid>
		<hbox>
			<button label="OK" forward="onOK()"/>
			<button label="Cancle" forward="onCancle()"/>
		</hbox>
	</vbox>
</window>

 

 task.zul背后的类PopWindow.java:

 

package cn.wempire.zkcrud.web.ui;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import cn.wempire.zkcrud.pojo.User;
import cn.wempire.zkcrud.service.UserService;

public class PopWindow extends Window
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected UserService userService;
	protected User user;

	public User getUser()
	{
		return user;
	}

	public void setUser(User user)
	{
		this.user = user;
	}

	public void onCreate()
	{
		if (user != null)
		{
			// update
			Textbox ctrl = (Textbox) this.getFellow("LoginName");
			ctrl.setValue(user.getLoginName());
			ctrl = (Textbox) this.getFellow("Password");
			ctrl.setValue(user.getPassword());
		}

		// spring bean
		ApplicationContext ctx = WebApplicationContextUtils
				.getRequiredWebApplicationContext((ServletContext) getDesktop()
						.getWebApp().getNativeContext());
		userService = (UserService) ctx.getBean("userService");
	}

	public void onOK() throws Exception
	{
		if (user == null)
		{
			// new
			user = new User();

			Textbox ctrl = (Textbox) this.getFellow("LoginName");
			user.setLoginName(ctrl.getValue());
			ctrl = (Textbox) this.getFellow("Password");
			user.setPassword(ctrl.getValue());

			userService.save(user);
		}
		else
		{
			// update
			Textbox ctrl = (Textbox) this.getFellow("LoginName");
			user.setLoginName(ctrl.getValue());
			ctrl = (Textbox) this.getFellow("Password");
			user.setPassword(ctrl.getValue());

			userService.update(user);
		}

		this.setAttribute("OK", Boolean.TRUE);
		this.detach();
	}

	public void onCancle()
	{
		this.detach();
	}
}

 注意PopWindow中得到Spring类的方式:

ApplicationContext ctx = WebApplicationContextUtils
.getRequiredWebApplicationContext((ServletContext) getDesktop()
.getWebApp().getNativeContext());
userService = (UserService) ctx.getBean("userService");

你可能感兴趣的:(spring,jsp,Hibernate,UI,zk)