Spring MVC or Wicket?

user1241320 picture user1241320 · Feb 29, 2012 · Viewed 13.6k times · Source

I have a long (and happy so far) experience with Spring MVC, but lately I'm getting interested in Wicket.

My question is also on how to handle (with Wicket) DI, Transaction Mgmt, JDBC connections and all that stuff? Is it okay to mix certain parts of the Springsource suite with Wicket? Wicket & Weld? Wicket & Guice?

Answer

tetsuo picture tetsuo · Mar 1, 2012

Wicket is a presentation-layer framework. It will not handle DI, transactions or connections.

But it can be easily integrated with a number of frameworks, including Spring, Guice (official Wicket modules, wicket-spring and wicket-guice) and CDI/Weld (wicket-cdi, a side project from Igor Vaynberg, one of the Wicket committers).

Wicket Wiki: Integration guides

Below, a simple Wicket application with Spring. The transaction bits are plain old Spring configuration, so I didn't bother including them.

HelloWorldService.java

public class HelloWorldService {
    private String message;
    public void setMessage(String message) {
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
}

applicationContext.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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

  <bean id="helloWorldService" class="minimal.wicketspring.HelloWorldService">
    <property name="message" value="Hello, World!" />
  </bean>
</beans>

WicketApplication.java

public class WicketApplication extends WebApplication {
    @Override
    public void init() {
        super.init();
        getComponentInstantiationListeners().add(new SpringComponentInjector(this));
    }
    @Override
    public Class<HomePage> getHomePage() {
        return HomePage.class;
    }
}

HomePage.java

public class HomePage extends WebPage {
    @SpringBean
    private HelloWorldService helloWorldService;

    public HomePage() {
        add(new FeedbackPanel("feedback"));
        add(new Link<Void>("link") {
            @Override
            public void onClick() {
                info(helloWorldService.getMessage());
            }
        });
    }
}

HomePage.html

<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<body>
  <div wicket:id="feedback"></div>
  <a wicket:id="link">Show Message</a>
</body>
</html>

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

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

  <filter>
    <filter-name>wicket.wicket-spring</filter-name>
    <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
    <init-param>
      <param-name>applicationClassName</param-name>
      <param-value>minimal.wicketspring.WicketApplication</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>wicket.wicket-spring</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>