Integrate ZK Spreadsheet2.0.0 with Spring"

From Documentation
(Created page with '{{Template:Smalltalk_Author| |author=Tony Wang, Engineer, Potix Corporation |date=Nov 4, 2010 |version=Applicable to ZK 5.0.4 and later ::Applicable to ZK Spreadsheet EE 2.0.0 …')
 
m (correct highlight (via JWB))
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{Template:Smalltalk_Author|
 
{{Template:Smalltalk_Author|
 
|author=Tony Wang, Engineer, Potix Corporation
 
|author=Tony Wang, Engineer, Potix Corporation
|date=Nov 4, 2010
+
|date=December, 2010
 
|version=Applicable to ZK 5.0.4 and later
 
|version=Applicable to ZK 5.0.4 and later
  
Line 9: Line 9:
 
=Introduction=
 
=Introduction=
  
In [http://docs.zkoss.org/wiki/Use_JavaBean_in_Excel_with_ZK_Spreadsheet_2.0.0 Use JavaBean in Excel with ZK Spreadsheet2] and [http://docs.zkoss.org/wiki/Change_the_Style_of_Spreadsheet_within_1_Min_2.0.0 Change the Style of Spreadsheet2 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.  
+
In [[Small_Talks/2010/December/Use_JavaBean_in_Excel_with_ZK_Spreadsheet_2.0.0 | Use JavaBean in Excel with ZK Spreadsheet2]] and [[Small_Talks/2010/December/Change_the_Style_of_Spreadsheet_within_1_Min_2.0.0 | Change the Style of Spreadsheet2 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.  
  
 
Let's start to build an application for integrating '''ZK Spreadsheet''' and '''Spring'''.
 
Let's start to build an application for integrating '''ZK Spreadsheet''' and '''Spring'''.
Line 17: Line 17:
 
It is similar to the demo in previous article. It demonstrates that you can achieve same functionality by integrating ZK Spreadsheet with Spring.
 
It is similar to the demo in previous article. It demonstrates that you can achieve same functionality by integrating ZK Spreadsheet with Spring.
  
<gflash>800 500 http://docs.zkoss.org/images/1/1a/ZssSpring.swf</gflash>
+
<gflash width="800" height="500"> ZssSpring.swf</gflash>
 
 
  
 
=Configuration Files=
 
=Configuration Files=
Line 27: Line 26:
 
'''web.xml'''
 
'''web.xml'''
  
<source lang="xml" high="3,4,5,6,7,8,9" >
+
<source lang="xml" highlight="3,4,5,6,7,8,9" >
 
......
 
......
  
Line 77: Line 76:
 
'''sheet.zul'''
 
'''sheet.zul'''
  
<source lang="xml" high="2">
+
<source lang="xml" highlight="2">
 
<!-- Add Variable-Resolver for Spring -->
 
<!-- Add Variable-Resolver for Spring -->
 
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
 
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
Line 122: Line 121:
 
=Get Values via Variable-Resolver In SpreadSheet=
 
=Get Values via Variable-Resolver In SpreadSheet=
  
You can access the variable resolver in spreadsheet , as we just mentioned in [http://docs.zkoss.org/wiki/Use_JavaBean_in_Excel_with_ZK_Spreadsheet_2.0.0 Use JavaBean in Excel with ZK Spreadsheet2] , so of course you can use spring DelegatingVariableResolver in zk spreadsheet .As the demo we shows up , you can take a java bean (or any java bean you want ),  
+
You can access the variable resolver in spreadsheet , as we just mentioned in [[Small_Talks/2010/December/Use_JavaBean_in_Excel_with_ZK_Spreadsheet_2.0.0 | Use JavaBean in Excel with ZK Spreadsheet2]]
 +
, so of course you can use spring DelegatingVariableResolver in zk spreadsheet .As the demo we shows up , you can take a java bean (or any java bean you want ),  
 
by setting  "=<bean-name>.<bean attibutes>"  in zk spreadsheet cell  , and variable resolver will find this bean's value.  
 
by setting  "=<bean-name>.<bean attibutes>"  in zk spreadsheet cell  , and variable resolver will find this bean's value.  
  
 
In this case , "=reporter.name" is just like set the cell value with  "((User)SpringUtil.getBean("reporter")).getName()" .
 
In this case , "=reporter.name" is just like set the cell value with  "((User)SpringUtil.getBean("reporter")).getName()" .
 
  
 
=Summary=
 
=Summary=
Line 142: Line 141:
 
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/Use_JavaBean_in_Excel_with_ZK_Spreadsheet_2.0.0 Use JavaBean in Excel with ZK Spreadsheet2]  
+
* [[Small_Talks/2010/December/Use_JavaBean_in_Excel_with_ZK_Spreadsheet_2.0.0 | Use JavaBean in Excel with ZK Spreadsheet2]]  
* [http://docs.zkoss.org/wiki/Change_the_Style_of_Spreadsheet_within_1_Min_2.0.0 Change the Style of Spreadsheet2 within 1 Min]
+
 
 +
* [[Small_Talks/2010/December/Change_the_Style_of_Spreadsheet_within_1_Min_2.0.0 | Change the Style of Spreadsheet2 within 1 Min]]
  
* [http://docs.zkoss.org/wiki/New_Features_of_ZK_Spreadsheet_2.0.0_RC New_Features_of_ZK_Spreadsheet_2.0.0_RC]
+
* [[Small_Talks/2010/December/New_Features_of_ZK_Spreadsheet_2.0.0_RC | New Features of ZK Spreadsheet 2.0.0 RC]]
  
* [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]]
  
  

Latest revision as of 04:18, 20 January 2022

DocumentationSmall Talks2010DecemberIntegrate ZK Spreadsheet2.0.0 with Spring
Integrate ZK Spreadsheet2.0.0 with Spring

Author
Tony Wang, Engineer, Potix Corporation
Date
December, 2010
Version
Applicable to ZK 5.0.4 and later
Applicable to ZK Spreadsheet EE 2.0.0

Introduction

In Use JavaBean in Excel with ZK Spreadsheet2 and Change the Style of Spreadsheet2 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.

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>
       <!-- 
              Note here the id("dataBase") will be used as a variable after , you can access dataBean in zscript and spreadsheet .  
              In this demo the database bean is a user-customize java class , it's a factory  for DataBean ,
              it will provide several databean , just like what a generic data access object does.
        -->
	<bean id="database" class="model.Database" />

        <!-- Here we take a POJO for a user which having a name , and set the name here , we will use it in the BalanceSheet.xls , 
               by setting  "=reporter.name"  in cell  , and variable resolver will find this bean's value. 
               That means you can set up a java bean through spring , and it will be accessed in zk shreadsheet , without any databinding jobs.
        --> 
        <bean id="reporter" class="model.User" >
           <property name="name" value="Tony" />
       </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>
Quarter:
  <listbox id="quarter" mold="select" rows="1" onSelect="updateQuarter()">
  	<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>
Style:
  <listbox id="style" mold="select" rows="1" onSelect="changeWorksheet()">
  	<listitem label="Style 1" value="/BalanceSheet.xls"/>
  	<listitem label="Style 2" value="/NewSheet.xls"/>
  </listbox>
<zscript>
	import model.*;
	
        //Here the database resolved from variable resolver. 
	DataBean dataBean = database.getQuarter(1);
	
	void updateQuarter() {
		Listitem listitem = quarter.getSelectedItem();
		int quarter = Integer.parseInt(listitem.getValue());  // get the selected value
		
		//update the  variable  
		dataBean = database.getQuarter(quarter);
		
		//notify the spreadsheet to update the variable value in cells
		balance.getBook().notifyChange(new String[]{"dataBean"});
	}
	
	void changeWorksheet() {  //Change the URL of spreadsheet , that means it will open another spreadsheet file.
		balance.setSrc(style.getSelectedItem().getValue().toString());
	}
	
</zscript>
	<spreadsheet id="balance" src="/BalanceSheet.xls" maxrows="40" maxcolumns="20" height="600px" width="1300px"/> 
</window>

Get Values via Variable-Resolver In SpreadSheet

You can access the variable resolver in spreadsheet , as we just mentioned in Use JavaBean in Excel with ZK Spreadsheet2 , so of course you can use spring DelegatingVariableResolver in zk spreadsheet .As the demo we shows up , you can take a java bean (or any java bean you want ), by setting "=<bean-name>.<bean attibutes>" in zk spreadsheet cell , and variable resolver will find this bean's value.

In this case , "=reporter.name" is just like set the cell value with "((User)SpringUtil.getBean("reporter")).getName()" .

Summary

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 .


Download


Reference

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:




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