Inject ZK Components in Spring Beans

From Documentation
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 ZK_Spring_Essentials/Getting_Started_with_ZK_Spring/Setting_Up_ZK_Spring Setting up ZK Spring 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-config/> 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

Note: This feature has a runtime dependency on reflections (v0.9.5-RC2) and javassist (v3.14.0.GA) libraries.

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

We have a Spring managed component bean that we apply as a controller to the window declared in our ZUML. We also autowire several ZK components in this bean as shown below.

@org.springframework.stereotype.Component("greetingCtrl")
@Scope("prototype")
public class GreetingCtrl extends GenericSpringComposer {

    @Autowired
    private Textbox name;
    
    @Autowired
    private Button greetBtn;
    
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
    }
    
    @EventHandler("greetBtn.onClick")
    public void showGreeting(Event evt) throws WrongValueException, InterruptedException {
        Messagebox.show("Hello " + name.getValue() + "!");
    }
}

As you can see above the @Autowired ZK components corresponds to the ones defined on the ZUML page. For this autowiring to work you need to extend from a new utility class org.zkoss.spring.util.GenericSpringComposer. In addition to the @Autowired ZK components in this Spring managed bean you can also define event handlers for ZK component events using EventHandler annotation. For example in above code we have an event handling method called showGreeting(Event) that handles onClick event of greetBtn ZK Button component.


Version History

Last Update : 2011/02/25


Version Date Content
     


Last Update : 2011/02/25

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