Integrate ZK Spreadsheet with Spring

From Documentation
Revision as of 06:09, 7 December 2010 by Char (talk | contribs) (→‎Introduction)
DocumentationSmall Talks2008MayIntegrate ZK Spreadsheet with Spring
Integrate ZK Spreadsheet with Spring

Author
Ivan Cheng, Engineer, Potix Corporation
Date
May 6, 2008
Version
Applicable to ZK 3.0.5 and later
Applicable to ZK Spreadsheet 1.0.0 and later


Introduction

In Use JavaBean in Excel with ZK Spreadsheet

and  Change the Style of Spreadsheet within 1 Min

, we demonstrate how to use JavaBean directly via Variable-Resolver and change the style of spreadsheet with ease. In this article, we will integrate ZK Spreadsheet and Spring to achieve same functionality. It is easy to integrate ZK Spreadsheet with Spring.

If you'd like to know more about ZK Spreadsheet, please refer to the following URLs:


If you'd like to know more about Variable-Resolver, please refer to the following URL:


Let's start to build an application for integrating ZK Spreadsheet and Spring.

Live Demo

It is similar to the demo in previous article. It demonstrates that you can achieve same functionality by integrating ZK Spreadsheet with Spring.

Configuration Files

In your web.xml, you have to use org.springframework.web.context.ContextLoaderListener to load the spring configuration when the server started.


web.xml

......

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/spring-config.xml</param-value>
</context-param>
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

......


Define your bean in Spring's configuration file. Please remember the bean id (dataBean, database) which will be taken as a key for Variable-Resolver to lookup the bean. I will describe it later.


spring-config.xml

......

<beans>
	<bean id="dataBean" class="model.DataBean"/>
	<bean id="database" class="model.Database">
		<property name="dataBean">
			<ref bean="dataBean"/>
		</property>
	</bean>
</beans>

......


Get Values via Variable-Resolver

ZK provides a variable-resolver directive that application developers can specifies the resolver class that will be used by the zscript interpreter to resolve unknown variables. Based on such mechanism, the ZK Team has implemented a variable resolver class that would automatically resolve the Spring bean objects by their ids defined in spring-config.xml . Thus the zul page now can use the Spring beans seamlessly in zscript or EL expression just like using general ZK components.


sheet.zul

//Add Variable-Resolver for Spring
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<window>
  <listbox id="quarter" mold="select" rows="1" onSelect="refreshQuarter()">
	<listitem value="0" label="Select"/>
	<listitem value="1" label="Quarter 1"/>
	<listitem value="2" label="Quarter 2"/>
	<listitem value="3" label="Quarter 3"/>
	<listitem value="4" label="Quarter 4"/>
  </listbox>
  <listbox id="style" mold="select" rows="1" onSelect="changeStyle()">
	<listitem label="Style 1" value="/BalanceSheet.xls"/>
	<listitem label="Style 2" value="/NewSheet.xls"/>
  </listbox>
<zscript>
	void refreshQuarter() {
		Listitem listitem = quarter.getSelectedItem();
		int quarter = Integer.parseInt(listitem.value);
		//Call the method to reload data
		database.reloadQuarter(quarter);
		//Call the method to refresh values of cells
		balance.book.notifyChange(new String[]{"dataBean"});
	}
	
	void changeStyle() {  //Change the URL of spreadsheet
		balance.url = style.getSelectedItem().value;
	}
	
	//Initial data, ZK will find it via DelegatingVariableResolver.
	database.reloadQuarter(0);
</zscript>
	<spreadsheet id="balance" url="/BalanceSheet.xls" maxrow="40" maxcolumn="20" height="600px" width="1300px"/> 
</window>


Download

Conclusion

The ZK team has implemented a Spring bean variable resolver which allows accessing spring bean directly in zscript and EL expression. In the same way we can get Spring beans in ZK Spreadsheet. All we have to do is to modify configuration files and then, layout the Spreadsheet at proper place. In the near future, we will introduce more useful tools of ZK Spreadsheet. We expect your response to make it better, feel free to leave a message or post it to ZK forum .




Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.