Using Custom ZK Scopes For Spring Beans"

From Documentation
Line 8: Line 8:
  
 
===Configuration===
 
===Configuration===
 +
 +
==== Java Config ====
 +
 +
<source lang="java" high="4">
 +
import org.zkoss.spring.config.ZkScopesConfigurer;
 +
 +
@Configuration
 +
@Import(ZkScopesConfigurer.class)
 +
public class YourApplicationConfiguration {
 +
    ...
 +
}
 +
</source>
 +
 +
==== XML Config ====
 +
 +
<source lang="xml" high="4,6,9">
 +
<?xml version="1.0" encoding="UTF-8"?>
 +
<beans xmlns="http://www.springframework.org/schema/beans"
 +
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 +
  xmlns:zksp="http://www.zkoss.org/2008/zkspring/core"
 +
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 +
            http://www.zkoss.org/2008/zkspring/core http://www.zkoss.org/2008/zkspring/core/zkspring-core-4.0.xsd">
 +
 +
<!-- Enables ZK custom scopes for Spring Beans -->
 +
<zksp:zk-config/>
 +
</beans>
 +
</source>
 +
 +
==== Previous version 3.2 (or earlier) ====
 +
 
To enable ZK custom scopes for your Spring Beans all you need is to add <code><zk-config/></code> element to your spring bean configuration file. Here is snippet from our sample application configuration
 
To enable ZK custom scopes for your Spring Beans all you need is to add <code><zk-config/></code> element to your spring bean configuration file. Here is snippet from our sample application configuration
<source lang="xml" high="6,10,16">
+
<source lang="xml" high="5,9,12">
 
<?xml version="1.0" encoding="UTF-8"?>
 
<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="http://www.springframework.org/schema/beans"
 
<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:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
     xmlns:context="http://www.springframework.org/schema/context"
 
     xmlns:context="http://www.springframework.org/schema/context"
 
     xmlns:zksp="http://www.zkoss.org/2008/zkspring/core"
 
     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
 
     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.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.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>
 
 
     <!-- Enables ZK custom scopes for Spring Beans -->
 
     <!-- Enables ZK custom scopes for Spring Beans -->
 
     <zksp:zk-config/>
 
     <zksp:zk-config/>

Revision as of 09:35, 21 January 2019

Using Custom ZK Scopes For Spring Beans



Purpose

Use ZK custom scope for Spring beans.

Example

In this example we will present user to enter a message which can be set into a Spring bean. This bean is set to custom ZK Spring desktop scope[1] so that it can be shared by other pages that are attached to this same desktop.

  1. Besides desktop scope, ZK Spring also support page scope, idspace scope, component scope, and execution scope

Configuration

Java Config

import org.zkoss.spring.config.ZkScopesConfigurer;

@Configuration
@Import(ZkScopesConfigurer.class)
public class YourApplicationConfiguration {
    ...
}

XML Config

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:zksp="http://www.zkoss.org/2008/zkspring/core"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.zkoss.org/2008/zkspring/core http://www.zkoss.org/2008/zkspring/core/zkspring-core-4.0.xsd">

	<!-- Enables ZK custom scopes for Spring Beans -->
	<zksp:zk-config/>
</beans>

Previous version 3.2 (or earlier)

To enable ZK custom scopes for your Spring Beans all you need is to add <zk-config/> element to your spring bean configuration file. Here is snippet from our sample application configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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"

    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">

    <!-- Enables ZK custom scopes for Spring Beans -->
    <zksp:zk-config/>
...

Note that you need to declare ZK Spring Core namespace at the start of your configuration file.

ZUML

Let's take a look at the main page source

<?xml version="1.0" encoding="UTF-8"?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<window title="Custom Scopes Example - Main Page" border="normal" height="100px"
    width="400px" apply="${scopesCtrl}">
    <label value="Enter Message Text:"></label>
    <textbox id="name" />
    <button id="setMsgBtn" label="Set" />
    <button id="showMsgBtn" label="Show Message" />
    <button id="showPageBtn" label="Show Window">
        <attribute name="onClick">
        Window win = (Window) Executions.getCurrent().createComponents("customScopesWindow.zul", null, null);
        win.doHighlighted();
        </attribute>
    </button>
</window>

It has one textbox to enter a message. On clicking Set button we store this text in a SimpleMessageBean which is Autowired into scopesCtrl controller.

Java

Here is the controller source in which SimpleMessageBean is autowired.

@org.springframework.stereotype.Component("scopesCtrl")
@Scope("desktop")
public class ScopesController extends GenericSpringComposer {

    @Autowired
    private SimpleMessageBean msgBean;
    @Autowired
    private Textbox name;
    @Autowired
    private Button setMsgBtn;
    @Autowired
    private Button showMsgBtn;

    @EventHandler("setMsgBtn.onClick")
    public void setMessage(Event evt) {
        msgBean.setMsg(name.getValue());
    }
    
    @EventHandler("showMsgBtn.onClick")
    public void showMessage(Event evt) throws InterruptedException {
        Messagebox.show(msgBean.getMsg());
    }
}

Here is the SimpleMessageBean source and you can see its scope is set to "desktop" scope using Scope annotation.

@org.springframework.stereotype.Component("msgBean")
@Scope("desktop")
public class SimpleMessageBean {

    private String msg;
    
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}

Now when you click Show window button on the main page it creates a new page called customScopesWindow.zul and attaches it to the current desktop. Here is the source code for this page

<?xml version="1.0" encoding="UTF-8"?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<window title="Custom Scopes Example - Window Page" border="normal" height="100px"
    width="400px" closable="true">
    <label value="Message Text:"></label>
    <textbox id="name" value="${msgBean.msg}"/>
</window>

Since it is attached to the same desktop we have access SimpleMessageBean instance stored in the desktop scope. We use our standard approach to use DelegatingVariableResolver to access such bean and assign its value to a textbox.

Download

Please down the source

Version History

Last Update : 2019/01/21


Version Date Content
     


Last Update : 2019/01/21

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