Integrate ZK Spreadsheet with Spring"

From Documentation
Line 10: Line 10:
 
=Introduction=
 
=Introduction=
  
In [http://docs.zkoss.org/index.php?title=Use_JavaBean_in_Excel_with_ZK_Spreadsheet Use JavaBean in Excel with ZK Spreadsheet] and [http://docs.zkoss.org/index.php?title=Change_the_Style_of_Spreadsheet_within_1_Min 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.
+
In [[Small_Talks/2008/April/Use_JavaBean_in_Excel_with_ZK_Spreadsheet | Use JavaBean in Excel with ZK Spreadsheet]]
 +
and [[Small_Talks/2008/April/Change_the_Style_of_Spreadsheet_within_1_Min | 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 ZK Spreadsheet, please refer to the following URLs:
  
* [http://docs.zkoss.org/wiki/New_Features_of_ZK_Spreadsheet_0.7.0 New Features of ZK Spreadsheet 0.7.0]
+
* [[Small_Talks/2008/February/New_Features_of_ZK_Spreadsheet_0.7.0 | New Features of ZK Spreadsheet 0.7.0]]
  
* [http://docs.zkoss.org/wiki/A_Preview_of_ZK_Spreadsheet_Component A Preview of ZK Spreadsheet Component]
+
* [[Small_Talks/2007/December/A_Preview_of_ZK_Spreadsheet_Component | A Preview of ZK Spreadsheet Component]]
  
* [http://docs.zkoss.org/wiki/A_Sip_of_the_ZK_Spreadsheet_Component A Sip of the ZK Spreadsheet Component]
+
* [[Small_Talks/2007/September/A_Sip_of_the_ZK_Spreadsheet_Component | A Sip of the ZK Spreadsheet Component]]
  
* [http://docs.zkoss.org/wiki/How_to_Build_a_Rich_Excel_Report_with_ZK_Spreadsheet How to Build a Rich Excel Report with ZK Spreadsheet]
+
* [[Small_Talks/2008/April/How_to_Build_a_Rich_Excel_Report_with_ZK_Spreadsheet | How to Build a Rich Excel Report with ZK Spreadsheet]]
  
  
 
If you'd like to know more about Variable-Resolver, please refer to the following URL:
 
If you'd like to know more about Variable-Resolver, please refer to the following URL:
  
* [http://docs.zkoss.org/wiki/Using_ZK_JndiVariableResolver_-_Retrieve_Session_Beans_and_EntityManagerFactory Using ZK JndiVariableResolver - Retrieve Session Beans and EntityManagerFactory]
+
* [[Small_Talks/2007/November/Using_ZK_JndiVariableResolver_-_Retrieve_Session_Beans_and_EntityManagerFactory | Using ZK JndiVariableResolver - Retrieve Session Beans and EntityManagerFactory]]
  
  

Revision as of 06:09, 7 December 2010

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.