2011년 11월 7일 월요일

zkoss spring configuration

ZK OSS는 Direct RIA 프레임워크다.
Direct RIA가 몬지. ZK가 몬지는 담에 기회에 있으면 포스팅하기로 하고,
이번 포스트는 zkoss에서 스프링을 사용하기 위한 설정만 살펴보기로 하자.

스프링 설정하는 건 다들 아는 것일테고, 간단히 zkoss에서 스프링 빈을 사용하는 방법에 대해서만 살펴보기로 하자.

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <description><![CDATA[springsample]]></description>
  <display-name>springsample</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      WEB-INF/zkoss-config.xml
    </param-value>
  </context-param>

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

 
  <servlet>
    <description>ZK loader for ZUML pages</description>
    <servlet-name>zkLoader</servlet-name>
    <servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>
 
    <init-param>
      <param-name>update-uri</param-name>
      <param-value>/zkau</param-value>
    </init-param>


    <load-on-startup>1</load-on-startup><!-- Must -->
  </servlet>
  <servlet-mapping>
    <servlet-name>zkLoader</servlet-name>
    <url-pattern>*.zul</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>zkLoader</servlet-name>
    <url-pattern>*.zhtml</url-pattern>
  </servlet-mapping>
 
  <servlet-mapping>
    <servlet-name>zkLoader</servlet-name>
    <url-pattern>/zk/*</url-pattern>
  </servlet-mapping>

  <servlet>
    <description>The asynchronous update engine for ZK</description>
    <servlet-name>auEngine</servlet-name>
    <servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>auEngine</servlet-name>
    <url-pattern>/zkau/*</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>dspLoader</servlet-name>
    <servlet-class>org.zkoss.web.servlet.dsp.InterpreterServlet</servlet-class>
    <init-param>
      <param-name>class-resource</param-name>
      <param-value>true</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>dspLoader</servlet-name>
    <url-pattern>*.dsp</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.zul</welcome-file>
    <welcome-file>index.zhtml</welcome-file>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
  </welcome-file-list>
</web-app>

web.xml 파일내에서 가장 중요한 것은 리스너 두개의 순서다. !!!
이거 무시하면 띠바 겁내 헤롱거릴거다!
반드시
org.zkoss.spring.web.context.CoreContextListener 먼저 설정 해주고
org.springframework.web.context.ContextLoaderListener 는 나중에 설정해 줘야한다.


나머지는 가이드 문서에 나와있는데로 설정하면 별 문제 없다.

zkoss-config.xml (web.xml 파일에서 지정한 파일)


<?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:p="http://www.springframework.org/schema/p"
  xmlns:zksp="http://www.zkoss.org/2008/zkspring/core"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:security="http://www.springframework.org/schema/security"
  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.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <context:annotation-config />
  <context:component-scan base-package="org.zkoss.zkspringessentials.controller,org.zkoss.spring.beans.zkcomponents,com.archnal.zkoss"></context:component-scan>
    <zksp:zk-config/>
</beans>


GreetingController.java

package com.archnal.zkoss.springsample;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.zkoss.spring.context.annotation.EventHandler;
import org.zkoss.spring.util.GenericSpringComposer;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Button;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;

@Component("greetingController")
@Scope("desktop")

public class GreetingController extends GenericSpringComposer {
  @Autowired
  private Textbox name;
 
  @Autowired
  private Button greetBtn;

  @Override
  public void doAfterCompose(org.zkoss.zk.ui.Component comp) throws Exception {
    super.doAfterCompose(comp);
  }
 
  @EventHandler("greetBtn.onClick")
  public void showGreeting(Event event) throws WrongValueException, InterruptedException {
    System.out.println("showGreeting");
    Messagebox.show("Hello " + name.getValue() + "!");
  }
}


greeting.zul

<?page title="Greeting " contentType="text/html;charset=UTF-8"?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<window title="Autowire ZK Components Example" border="normal"
  height="100px" width="400px" apply="${greetingController }">

  <label value="Name:"></label>
  <textbox id="name"/>
  <button id="greetBtn" label="Greet!" />
</window>

 

위와 같이 설정하면 zkoss에서 스프링 프레임워크를 마음 껏 사용할 수 있다.~~