Integrate ZK Spreadsheet with Spring"
m (correct highlight (via JWB)) |
|||
(6 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{ZKSpreadsheetEssentialsPageHeader}} | {{ZKSpreadsheetEssentialsPageHeader}} | ||
+ | |||
+ | |||
+ | {{Deprecated|url=http://books.zkoss.org/wiki/ZK_Spreadsheet_Essentials}} | ||
+ | |||
__TOC__ | __TOC__ | ||
+ | {{ZSS EE}} | ||
ZK Spreadsheet can resolve the name expressions in cells to bind the data from the Spring-managed beans automatically. | ZK Spreadsheet can resolve the name expressions in cells to bind the data from the Spring-managed beans automatically. | ||
Line 18: | Line 23: | ||
For variables in cells, if valid Excel ''Defined Names'' found in template file, ZK Spreadsheet will use them. If not, ZK Spreadsheet follows ZK's EL expression variable resolving mechanism. It first tries to find any matching zscript variables defined in ZUML page. Then check ids of ZK fellow components. Then search in ZK components' attribute map. Finally ask variable resolvers defined in the zul page to retrieve the bean of the named variable. If still none is found, it will return ''#NAME?'' as Excel's original behavior. Once variables are resolved, the associated getter are called and value returned in the cell. | For variables in cells, if valid Excel ''Defined Names'' found in template file, ZK Spreadsheet will use them. If not, ZK Spreadsheet follows ZK's EL expression variable resolving mechanism. It first tries to find any matching zscript variables defined in ZUML page. Then check ids of ZK fellow components. Then search in ZK components' attribute map. Finally ask variable resolvers defined in the zul page to retrieve the bean of the named variable. If still none is found, it will return ''#NAME?'' as Excel's original behavior. Once variables are resolved, the associated getter are called and value returned in the cell. | ||
− | So simply declare the < | + | So simply declare the <code>variable-resolver</code> with <code>org.zkoss.zkplus.spring.DelegatingVariableResolver</code> on top of your ZUML page, you are now data-binding your ZK Spreadsheet with your Spring-managed beans directly. |
===ZUML=== | ===ZUML=== | ||
In ZUML page, we declare the variable resolver for retrieving Spring-managed beans so ZK Spreadsheet can find them and show the evaluated value in the spreadsheet's cells. | In ZUML page, we declare the variable resolver for retrieving Spring-managed beans so ZK Spreadsheet can find them and show the evaluated value in the spreadsheet's cells. | ||
− | <source lang="xml" | + | <source lang="xml" highlight="1"> |
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?> | <?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?> | ||
<spreadsheet id="bsheet" | <spreadsheet id="bsheet" | ||
− | src="/bsheet.xls" | + | src="/WEB-INF/excel/bean/bsheet.xls" |
maxrows="200" | maxrows="200" | ||
maxcolumns="40" | maxcolumns="40" | ||
Line 35: | Line 40: | ||
===Spring Configuration File=== | ===Spring Configuration File=== | ||
− | Define bean definitions in < | + | Define bean definitions in <code>spring-config.xml</code> file, and put it into your <code>WEB-INF</code> directory. |
− | <source lang="xml" | + | <source lang="xml" highlight="5"> |
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> | <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> | ||
Line 44: | Line 49: | ||
<bean id="dataBean" class="org.zkoss.zssessentials.bean.spring.DataBeanImpl"/> | <bean id="dataBean" class="org.zkoss.zssessentials.bean.spring.DataBeanImpl"/> | ||
</beans> | </beans> | ||
+ | </source> | ||
+ | |||
+ | ===Web Configuration File=== | ||
+ | In web.xml, a typical configuration for Spring Framework. | ||
+ | |||
+ | <source lang="xml" highlight="4"> | ||
+ | ... | ||
+ | <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> | ||
+ | ... | ||
</source> | </source> | ||
Line 49: | Line 69: | ||
Define DataBean interface and its implementation. Mainly getters and setters for various attributes for a balance sheet. | Define DataBean interface and its implementation. Mainly getters and setters for various attributes for a balance sheet. | ||
− | < | + | <code>''DataBean.java''</code> |
− | <source lang="java" | + | <source lang="java" highlight="3"> |
package org.zkoss.zssessentials.bean.spring; | package org.zkoss.zssessentials.bean.spring; | ||
public interface DataBean { | public interface DataBean { | ||
Line 62: | Line 82: | ||
</source> | </source> | ||
− | < | + | <code>''DataBeanImpl.java''</code> |
− | <source lang="java" | + | <source lang="java" highlight="17"> |
package org.zkoss.zssessentials.bean.spring; | package org.zkoss.zssessentials.bean.spring; | ||
public class DataBeanImpl implements DataBean { | public class DataBeanImpl implements DataBean { | ||
Line 95: | Line 115: | ||
===Result=== | ===Result=== | ||
− | [[Image:bsheet-result.png]] | + | [[Image:bsheet-spring-result.png]] |
+ | |||
+ | ===When Value of Spring-managed Bean Changes=== | ||
+ | Will ZK Spreadsheet update the cells if the value of the Spring-managed bean changes? Sure, but you have to notify the ZK Spreadsheet book for such cases. | ||
+ | <source lang="java"> | ||
+ | bsheet.getBook().notifyChange(new String[] {"dataBean"}); | ||
+ | </source> | ||
+ | When you notify the ZK Spreadsheet work book model that Spring-managed beans with the specified names have changed, it will collect which cells are affected (i.e. those dependent cells with the specified Spring bean names), and publish data change event accordingly. The ZK Spreadsheet UI or any other components that have subscribed to the work book's event queue and are interested in the event will then retrieve the new cells' data from the beans and update themselves. | ||
=Version History= | =Version History= |
Latest revision as of 12:53, 19 January 2022
This article is out of date, please refer to http://books.zkoss.org/wiki/ZK_Spreadsheet_Essentials for more up to date information.
Available in ZK Spreadsheet EE only
ZK Spreadsheet can resolve the name expressions in cells to bind the data from the Spring-managed beans automatically.
Purpose
Data binding spreadsheet cells to Spring-managed beans.
Template Excel File with Proper Name Expressions
Construct an Excel template file with proper name expressions in cells.
You will see #NAME? in cells because Excel does not know such names. However, they will be interpreted when import into ZK Spreadsheet component.
How ZK Spreadsheet Resolve my Spring-managed Beans
For variables in cells, if valid Excel Defined Names found in template file, ZK Spreadsheet will use them. If not, ZK Spreadsheet follows ZK's EL expression variable resolving mechanism. It first tries to find any matching zscript variables defined in ZUML page. Then check ids of ZK fellow components. Then search in ZK components' attribute map. Finally ask variable resolvers defined in the zul page to retrieve the bean of the named variable. If still none is found, it will return #NAME? as Excel's original behavior. Once variables are resolved, the associated getter are called and value returned in the cell.
So simply declare the variable-resolver
with org.zkoss.zkplus.spring.DelegatingVariableResolver
on top of your ZUML page, you are now data-binding your ZK Spreadsheet with your Spring-managed beans directly.
ZUML
In ZUML page, we declare the variable resolver for retrieving Spring-managed beans so ZK Spreadsheet can find them and show the evaluated value in the spreadsheet's cells.
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<spreadsheet id="bsheet"
src="/WEB-INF/excel/bean/bsheet.xls"
maxrows="200"
maxcolumns="40"
vflex="1"
width="100%">
</spreadsheet>
Spring Configuration File
Define bean definitions in spring-config.xml
file, and put it into your WEB-INF
directory.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataBean" class="org.zkoss.zssessentials.bean.spring.DataBeanImpl"/>
</beans>
Web Configuration File
In web.xml, a typical configuration for Spring Framework.
...
<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>
...
Spring-managed Bean
Define DataBean interface and its implementation. Mainly getters and setters for various attributes for a balance sheet.
DataBean.java
package org.zkoss.zssessentials.bean.spring;
public interface DataBean {
public double getLiquidAssets();
public void setLiquidAssets(double liquidAssets);
//...other getters and setters
}
DataBeanImpl.java
package org.zkoss.zssessentials.bean.spring;
public class DataBeanImpl implements DataBean {
private double liquidAssets;
private double fundInvestment;
private double fixedAssets;
private double intangibleAsset;
private double otherAssets;
private double currentLiabilities;
private double longTermLiabilities;
private double otherLiabilities;
private double capitalStock;
private double capitalSurplus;
private double retainedEarnings;
private double otherEquity;
private double treasuryStock;
public double getLiquidAssets() {
return liquidAssets;
}
public void setLiquidAssets(double liquidAssets) {
this.liquidAssets = liquidAssets;
}
//...other getters and setters implementation
}
Result
When Value of Spring-managed Bean Changes
Will ZK Spreadsheet update the cells if the value of the Spring-managed bean changes? Sure, but you have to notify the ZK Spreadsheet book for such cases.
bsheet.getBook().notifyChange(new String[] {"dataBean"});
When you notify the ZK Spreadsheet work book model that Spring-managed beans with the specified names have changed, it will collect which cells are affected (i.e. those dependent cells with the specified Spring bean names), and publish data change event accordingly. The ZK Spreadsheet UI or any other components that have subscribed to the work book's event queue and are interested in the event will then retrieve the new cells' data from the beans and update themselves.
Version History
Version | Date | Content |
---|---|---|
All source code listed in this book is at Github.