Inject ZK Components in Spring Beans"

From Documentation
m
m
Line 1: Line 1:
 
{{ZKSpringEssentialsPageHeader}}
 
{{ZKSpringEssentialsPageHeader}}
 +
 +
Starting from ZK Spring 4.0.0 this feature has been removed.
  
 
{{Deprecated|url= http://blog.zkoss.org/index.php/2012/03/08/zk-spring-3-1-release-introduction}}
 
{{Deprecated|url= http://blog.zkoss.org/index.php/2012/03/08/zk-spring-3-1-release-introduction}}
  
 
we deprecate this feature since ZK Spring 3.1 for its function might be unstable under various application servers, which means <tt> GenericSpringComposer</tt>, <tt> CoreContextListener</tt>, and <tt> @EventHandler </tt> are deprecated. Please refer to [http://blog.zkoss.org/index.php/2012/03/08/zk-spring-3-1-release-introduction a post on the blog]. We suggest users to use <tt>SelectorComposer</tt> or <tt> GenericForwardComposer </tt>. For more information, please refer http://books.zkoss.org/wiki/ZK_Developer's_Reference/MVC/Controller/Composer.
 
we deprecate this feature since ZK Spring 3.1 for its function might be unstable under various application servers, which means <tt> GenericSpringComposer</tt>, <tt> CoreContextListener</tt>, and <tt> @EventHandler </tt> are deprecated. Please refer to [http://blog.zkoss.org/index.php/2012/03/08/zk-spring-3-1-release-introduction a post on the blog]. We suggest users to use <tt>SelectorComposer</tt> or <tt> GenericForwardComposer </tt>. For more information, please refer http://books.zkoss.org/wiki/ZK_Developer's_Reference/MVC/Controller/Composer.
 
With ZK Spring 4.0.0 this feature has been removed.
 
  
 
===Purpose===
 
===Purpose===

Revision as of 09:54, 21 January 2019

Inject ZK Components in Spring Beans



Starting from ZK Spring 4.0.0 this feature has been removed.

Stop.png This article is out of date, please refer to http://blog.zkoss.org/index.php/2012/03/08/zk-spring-3-1-release-introduction for more up to date information.

we deprecate this feature since ZK Spring 3.1 for its function might be unstable under various application servers, which means GenericSpringComposer, CoreContextListener, and @EventHandler are deprecated. Please refer to a post on the blog. We suggest users to use SelectorComposer or GenericForwardComposer . For more information, please refer http://books.zkoss.org/wiki/ZK_Developer's_Reference/MVC/Controller/Composer.

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 section earlier. In addition to this you need the following configurations to make autowiring of ZK components in Spring beans work. In your application web.xml file, 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), javassist (v3.14.0.GA) and CGLIB (v2.2)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("desktop")
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 : 2019/01/21


Version Date Content
  3.1   2012/3/9   Deprecate the feature "Inject ZK Components in Spring Beans".
  4.0.0   2019/01   Remove the feature "Inject ZK Components in Spring Beans".


Last Update : 2019/01/21

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