Inject ZK Components in Spring Beans

From Documentation
Revision as of 04:47, 24 February 2011 by Ashishd (talk | contribs)
Inject ZK Components in Spring Beans



Purpose

Inject ZK Components in a Spring component bean

Example

This example demonstrates how Spring web application developers can autowire ZK components into spring component beans. It has a single textbox to enter a name and a button to show a greeting message.

Configuration

Setup ZK Spring integration library as described in "Setting up ZK Spring Integration Library" section earlier. In addition to this you need following configurations to make autowiring of ZK components in spring beans work. In your application web.xml declare ZK Spring Core listener. Make sure it is declared before standard Spring context listener as it needs to pre-process spring beans to enable ZK component autowiring.

    <listener>
        <listener-class>org.zkoss.spring.web.context.CoreContextListener</listener-class>
    </listener>

Next enable ZK custom scopes by specifying <zk-confgi /> in your bean configuration file

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:zksp="http://www.zkoss.org/2008/zkspring/core"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
             http://www.zkoss.org/2008/zkspring/core http://www.zkoss.org/2008/zkspring/core/zkspring-core.xsd
               http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <context:component-scan base-package="org.zkoss.zkspringessentials.controller,org.zkoss.spring.beans.zkcomponents"></context:component-scan>
    <zksp:zk-config/>
...

Notice that you need to declare ZK Spring Core namespace schema at the start of your bean configuration file. Also important is to enable context component scan for org.zkoss.spring.beans.zkcomponents package

ZUML

Lets take a look at our example ZUML file

<?xml version="1.0" encoding="UTF-8"?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<window title="Autowire ZK Components Example" border="normal" height="100px"
    width="400px" apply="${greetingCtrl}">
    <label value="Name:"></label>
    <textbox id="name" />
    <button id="greetBtn" label="Greet!" />
</window>

here we use standard ZK MVC approach to apply a controller to the main window using apply attribute. Value of apply attribute is an EL expression ${greetingCtrl} that resolves to a spring bean instance.

Java

Version History

Last Update : 2011/02/24


Version Date Content
     


Last Update : 2011/02/24

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