Monday, 20 June 2016

How to integrate Spring with Wicket ?


Wicket + Spring integration

Step 1. Add dependencies
For Spring and wicket integration we need to add wicket-spring , spring-core , spring-context, and spring-web dependency first like as follows.
<dependency>
     <groupId>org.apache.wicket</groupId>
     <artifactId>wicket-spring</artifactId>
     <version>6.15.0</version>
</dependency>           

<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-core</artifactId>
     <version>3.2.13.RELEASE</version>
</dependency>

<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>3.2.13.RELEASE</version>
</dependency>

<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-web</artifactId>
     <version>3.2.13.RELEASE</version>
</dependency>


Step 2.  Add Spring Context listener in the web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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"
       version="2.4">
  <display-name>Wicket Web Application</display-name>

  <filter>
    <filter-name>wicket.wicketTest</filter-name>
    <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
    <init-param>
        <param-name>applicationClassName</param-name>
        <param-value>com.mkyong.MyApplication</param-value>
    </init-param>
  </filter>

  <filter-mapping>
     <filter-name>wicket.wicketTest</filter-name>
     <url-pattern>/*</url-pattern>
  </filter-mapping>

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

</web-app>


Step 3. Make a new spring configuration file applicationContext.xml and initialize the bean properties like we do in spring as follows:
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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">

    <bean class="UserModel" id="userModel">
         <property name="name" value="Rahul"></property>
         <property name="gender" value="Male"></property>
    </bean>
</beans>


Step 4. After that we need to add component instantiation listner inside the init method and pass the Spring component injector in that.
public class MyApplication extends WebApplication {
    @Override
    public Class<? extends Page> getHomePage() {
        return WelcomePage.class; // return default page
    }
    @Override
    public void init() {
        super.init();
        addComponentInstantiationListener(
            new SpringComponentInjector(this));
    }
}


Step 5. After that you can use @SpringBean anootation for using any bean like SpringBean.
public class WelcomePage extends WebPage {
    @SpringBean(name="userModel")
    UserModel userModel;

    public WelcomePage() {
         add(new Label("namelbl",
             new PropertyModel<String>(userModel, "name")));
         add(new Label("gendrerlbl", 
             new PropertyModel<String>(userModel, "gender")));
    }
}

Step 6.  Use those Labels in the html file
<!DOCTYPE html>
<html xmlns="http://wicket.apache.org">
<head>
  <meta charset="ISO-8859-1">
  <title>Insert title here</title>
</head>
<body>
  <span wicket:id="namelbl"></span>
  <span wicket:id="gendrerlbl"></span>
</body>

</html>

No comments:

Post a Comment

Note: only a member of this blog may post a comment.