Spring-Hibernate-ZK

package com.zk;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.util.Composer;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;

import com.hibernate.entity.Info;
import com.service.Service;

/**
 * @author AnnBabyLive
 *
 */
public class MyComposer implements Composer {

	protected Textbox name;
	protected Textbox address;
	private Service service;
	
	
	public void setService(Service service) {
		this.service = service;
	}

	public void init(Component component){
		
		//将当前对象的变量和参数对象进行绑定
		Components.wireVariables(component, this);
		
		//转发一个当前的事件方法到所对应名称的Component()
		Components.addForwards(component, this);
		
		//注册一个监听到当前的对象
		Events.addEventListeners(component, this);
	}
	
	public void doAfterCompose(Component component) throws Exception {
		init(component);
	}
	
	/**
	 * 
	 * 
	 * onClick$save
	 *     onClick=事件名称
	 *        save=对应的Component id
	 * $必须的!
	 * 
	 */
	public void onClick$save(Event e){
		Info info=new Info();
		info.setName(name.getValue());
		info.setAddress(address.getValue());
		service.saveInfo(info);
		showMes("录入成功!");
	}
	
	public void showMes(String message){
		try {
			Messagebox.show(message);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">

	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url"
			value="jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8" />
		<property name="username" value="root" />
		<property name="password" value="root" />
	</bean>

	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<tx:annotation-driven transaction-manager="transactionManager" />

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation"
			value="/WEB-INF/hibernate.cfg.xml">
		</property>
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<bean id="baseHibernateDAO" class="com.hibernate.dao.BaseHibernateDAOImp">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	
	<bean id="infoDAO" class="com.hibernate.dao.InfoDAO" parent="baseHibernateDAO"/>
	
	<bean id="service" class="com.service.ServiceImp">
		<property name="infoDAO" ref="infoDAO"/>
	</bean>

	<bean id="myWindow" class="com.zk.MyComposer" scope="prototype">
		<property name="service" ref="service"/>
	</bean>

</beans>
 
<?page id="testZul" title=" New ZUL Title" cacheable="false" 
	language="xul/html" zscriptLanguage="Java" contentType="text/html;charset=UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>


<zk xmlns="http://www.zkoss.org/2005/zul"
	xmlns:h="http://www.w3.org/1999/xhtml" 
	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">
<window title="My First Window" border="normal" width="200px" apply="${myWindow}">

	<label value="Name:"/><textbox id="name"/>
	<label value="Address:"/><textbox id="address"/>
	<button id="save" label="Save"/>

</window>
</zk>
 

   

    一定要加<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>

    name,address,save一定要和MyComposer.class里面的一样

 

public class ServiceImp implements Service {

	private InfoDAO infoDAO;

	public void setInfoDAO(InfoDAO infoDAO) {
		this.infoDAO = infoDAO;
	}

	@Transactional(propagation=Propagation.REQUIRED,rollbackFor={RuntimeException.class})
	public void saveInfo(Info info) {
		this.infoDAO.save(info);
	}
	
}

  

    也可以让MyComposer extends GenericForwardComposer || GenericAutowireComposer

    然后再doAfterCompose调用父类的doAfterCompose方法,这样就不用去写init方法了!

    不过还是觉的写init 要比extends一个Class好!

 

    部分代码再上传的文件里

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