Integrate ZK Spreadsheet2.0.0 with Spring"
m |
(→Live Demo: fix live demo setting) |
||
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 | + | <gflash width="800" height="500"> ZssSpring.swf</gflash> |
− | |||
=Configuration Files= | =Configuration Files= |
Revision as of 07:45, 30 May 2012
Tony Wang, Engineer, Potix Corporation
December, 2010
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. |