一个较完整的zk操作后台实例

首先需要配好spring的配置文件,加载对zk的支持,可参考另一篇文章。
page.zul页面:
<!--读取国际化资源文件-->
<?taglib uri="WEB-INF/tld/web/core.dsp.tld" prefix="c"?>
<!--支持spring的管理-->
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<!--跟后台控制类的绑定,比如组件、集合等,arg0指明绑定组件的id-->
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="./anw" ?>
<zk xmlns="http://www.zkoss.org/2005/zul"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd">
<!--指明后台控制类的spring代理bean为:anwCtrl-->
	<window id="anw" apply="${anwCtrl}"
		title="${c:l('label.pro.anwCfg')}" border="normal">

		<groupbox mold="3d">
			<caption label="${c:l('lable.result')}"
				image="image/result.gif">
				<button id="add" label="${c:l('button.add')}" />
				<button id="edit" label="${c:l('button.update')}" />
				<button id="delete" label="${c:l('button.delete')}" />
			</caption>
			<listbox id="listbox_anw" checkmark="true" multiple="true"
				model="@{anwCtrl.anw_list}" mold="paging" pageSize="10">
				<listhead>
					<listheader label="${c:l('label.anw.name')}" />
					<listheader
						label="${c:l('label.anw.description')}" />
					<listheader label="id" visible="false" />
				</listhead>
				<listitem self="@{each=one}" value="@{one}">
					<listcell label="@{one.name}" />
					<listcell label="@{one.description}" />
					<listcell label="@{one.id}" visible="false" />
				</listitem>
			</listbox>
		</groupbox>
	</window>
</zk>


后台控制类AnwCtrl.java:
@Controller("anwCtrl")
@Scope("prototype")
public class AnwCtrl extends GenericForwardComposer{

	private final Log log = LogFactory.getLog(getClass());

  //组件变量名与page.zul的组件id相对应
	private Listbox listbox_anw;
	private Textbox tbox_name,tbox_descrip;

	@Resource
	private AnwService anwService;

	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
	}

  //getAnw_list对应page.zul中listbox的model="@{anwCtrl.anw_list}"
	public List<Anw> getAnw_list() {
		list = anwService.getAnwService();
	}

	public void onClick$edit() {
		if (1 == listbox_anw.getSelectedCount()) {
			Listitem item = listbox_anw.getSelectedItem();
			Anw e = (Anw) item.getValue();
			Map map = new HashMap();
		        map.put("selected", e);
              //在新窗口打开anw_dialog.zul,并向其传递参数map
			PageUtil.createModelWindow(Page.CONTAINER_RIGHT,
					Page.ANGWATTR_DIALOG, map);
		} 
	}
  //查询后刷新界面
	public void onClick$query() {
		List<Anw> list = anwService.queryAnw(
				tbox_name.getValue(), tbox_descrip.getValue());
		List<Anw> listModel = (List<Anw>) listbox_anw
					.getModel();
		listModel.clear();
		listModel.addAll(list);
	}
}


anw_dialog.zul的控制类:
@Controller("anwDialogCtrl")
@Scope("prototype")
public class AnwDialogCtrl extends GenericForwardComposer {

	private final Log log = LogFactory.getLog(getClass());

	private Longbox tbox_id;
	private Textbox tbox_name, tbox_descrip;
    //用于显示错误信息
	private Label error;

	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
        //获取从page.zul传来的参数map,在父类GenericForwardComposer中封装为arg
		if (null != arg && 0 < arg.size()) {
			Anw e = (Anw ) arg.get("selected");
			tbox_id.setValue(e.getId());
			tbox_name.setValue(e.getName());
			tbox_descrip.setValue(e.getDescription());
		}
	}
        ……
}


你可能感兴趣的:(spring,Web,bean,prototype,zk)