Wicket + Spring integration example

This tutorial demonstrates how to integrate Wicket with Spring framework.

Libraries in this article :

  1. Wicket v1.4.17
  2. wicket-spring v1.4.17
  3. Spring v3.0.5.RELEASE

1. Project Structure

Final project directory structure of this tutorial, nothing special, just a standard Maven project.

wicket spring example

2. Project Dependency

Get Wicket and Spring dependencies, to integrate both, you need “wicket-spring.jar“.


<project ..>

	<dependencies>

		<!-- Wicket framework-->
		<dependency>
			<groupId>org.apache.wicket</groupId>
			<artifactId>wicket</artifactId>
			<version>1.4.17</version>
		</dependency>

		<!-- Integrate Wicket with Spring -->
		<dependency>
			<groupId>org.apache.wicket</groupId>
			<artifactId>wicket-spring</artifactId>
			<version>1.4.17</version>
		</dependency>

		<!-- Spring framework -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>

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

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

		<!-- slf4j-log4j -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.5.6</version>
		</dependency>

	</dependencies>

</project>

3. Spring Bean

Create a Spring bean, annotate it with @Service.


package com.mkyong.user;

public interface HelloService {

	String getHelloWorldMsg();
	
}

package com.mkyong.user;

import org.springframework.stereotype.Service;

@Service
public class HelloServiceImpl implements HelloService {

	public String getHelloWorldMsg() {
		return "Spring : hello world";
	}

}

4. Inject into Spring container

Create a standard Spring applicationContext.xml file, enable the auto component scanning feature.

File : applicationContext.xml


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

	<context:component-scan base-package="com.mkyong.user" />
	
</beans>

5. Integrate Wicket with Spring

Override Wicket application init() method with this “addComponentInstantiationListener(new SpringComponentInjector(this));“.

File : Wicket application class


package com.mkyong;

import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import com.mkyong.user.SimplePage;

public class WicketApplication extends WebApplication {

	@Override
	public Class<SimplePage> getHomePage() {

		return SimplePage.class; // return default page
	}
	
	@Override
	protected void init() {
		
		super.init();
		addComponentInstantiationListener(new SpringComponentInjector(this));
		 
	}

}

Now, you can inject Spring bean into Wicket component via @SpringBean.


package com.mkyong.user;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.spring.injection.annot.SpringBean;

public class SimplePage extends WebPage {

	@SpringBean
	private HelloService helloService;
	
	public SimplePage(final PageParameters parameters) {

		add(new Label("msg", helloService.getHelloWorldMsg()));

	}

}

<html>
<body>
	<h1>Wicket + Spring integration example</h1>

	<h2 wicket:id="msg"></h2>
		
</body>
</html>

6. web.xml

Last step, make your web project know what is Wicket and Spring. Declares both in web.xml.

File : 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.WicketApplication</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>

7. Demo

Start and visit – http://localhost:8080/WicketExamples/.

A simple Wicket page, and the message is returned from Spring.

wicket spring demo

References

  1. Wicket + Spring detail explanation
  2. Spring Maven detail
  3. Spring auto componenet scanning

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
12 Comment threads
8 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
13 Comment authors
guest00001Nicolae CosmaTimAmir MeysamiDenise Recent comment authors
newest oldest most voted
guest00001
Guest
guest00001

With your provided maven file this app example is not working!!!!!
Just add extra * in includes part, should be ** instead of *

Nicolae Cosma
Guest
Nicolae Cosma

Here is a problem:

src/main/resources

false
src/main/java

* // should be **

**/*.java

Tim
Guest
Tim

With this example, will you not run into trouble with serialization?

Amir Meysami
Guest
Amir Meysami

0 down vote You should use wicket with the maven archetype, it sets you up to solve exactly this problem. Assuming you use maven (which you should), a source folder (like src/main/java) is not a resource folder (like src/main/resources). So if you need to copy resources from your source folder to the output, you need to add the source folder as a resource folder in your pom.xml: <build> <resources> <resource> <directory>src/main/java</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> </build>

Rajasekhar
Guest
Rajasekhar

Very good post on the wicket-spring integration. Can you please post a sample application on the wicket-Spring-Security?

Thanks in advance!!

Thierry
Guest
Thierry
akula
Guest
akula

to Mkyong: Thanks for nice article!
To Sfeher: I had the same issue and you solved it! Thank you!

Sonja
Guest
Sonja

Thanks for providing this tutorial.

For Wicket >= 1.5 a tiny bit has changed. The initialization line in the WicketApplication seems to have to look like this now (at least it only compiles like this):

getComponentInitializationListeners().add(new SpringComponentInjector(this));
Denise
Guest
Denise

Thank you so much for posting this, Sonja. It really was a big help for me. It looks there’s been a tiny typo though; if you use

 getComponentInstantiationListeners().add(new SpringComponentInjector(this)); 

everything works fine :)

Daniel
Guest
Daniel

Hello,

great example, straightforward and easy to follow. Thanks a lot!

sudheer
Guest
sudheer

you mentioned listener in web.xml but not mentioned the contextParam.How it works?

sfeher
Guest
sfeher

The article is very useful for me!

Anyway I downloaded and deployed the app but this throws me an exception:

(Netbeans 7,Tomcat 7.0.11, Glassfish 3.1)

Unexpected RuntimeException

WicketMessage: Markup of type 'html' for component 'com.mkyong.user.SimplePage' not found. Enable debug messages for org.apache.wicket.util.resource to get a list of all filenames tried.: [Page class = com.mkyong.user.SimplePage, id = 0, version = 0]

Root cause:

org.apache.wicket.markup.MarkupNotFoundException: Markup of type 'html' for component 'com.mkyong.user.SimplePage' not found. Enable debug messages for org.apache.wicket.util.resource to get a list of all filenames tried.: [Page class = com.mkyong.user.SimplePage, id = 0, version = 0]
at org.apache.wicket.markup.MarkupCache.getMarkupStream(MarkupCache.java:227)
at org.apache.wicket.MarkupContainer.getAssociatedMarkupStream(MarkupContainer.java:351)
at org.apache.wicket.Page.onRender(Page.java:1587)
at org.apache.wicket.Component.render(Component.java:2521)
at org.apache.wicket.Page.renderPage(Page.java:932)

trackback
Wicket + Kaptcha integration example

[…] Spring v3.0.5.RELEASE Note This article is mainly describes how to integrate Kaptcha with Wicket framework, for Wicket + Spring, please refer to this “Wicket + Spring integration example“. […]