I've been reading the Spring/Wicket integration threads on the mailing lists and looked at the code that is available but I was not entirily happy with the things I saw. So, stubborn as I am, I decided to forget everything and do some things 'my own way' instead >:-)

First, I embedded Jetty in Spring. This is actually something I did for a previous project where I needed an embedded web server that could run a servlet and serve XML-RPC. I really like this way of doing web applications/services as I don't have to deal with any arcane servlet archive and deployment cr*p that only adds complexity instead of making things easier.

This is a really simple example of a web server that simply serves static content on both 8080 and 8443 (SSL).

<bean name="myJettyServer" class="com.sateh.spring.jetty.JettyServer">

    <property name="listeners">
      <list>
        <bean class="org.mortbay.http.SocketListener">
          <property name="port" value="8088"/>
        </bean>
         <bean class="org.mortbay.http.SunJsseListener">
          <property name="port" value="8443"/>
          <property name="keystore" value="./keystore"/>
          <property name="password" value="test"/>
          <property name="keyPassword" value="test"/>
        </bean>
    </list>
    </property>

    <property name="contexts">
      <list>
<bean name="myContext" class="com.sateh.spring.jetty.JettyContext">
          <property name="contextPath" value="/test/*"/>
          <property name="resourceBase" value="./webroot"/>
          <property name="handlers">
            <list>
<bean class="com.sateh.spring.jetty.JettyResourceHandler"/>
              <bean class="org.mortbay.http.handler.NotFoundHandler"/>
            </list>
          </property>
        </bean>
      </list>
    </property>

  </bean>

I deploy this with:

  public static void main(String[] args)
  {
     new ClassPathXmlApplicationContext("spring-context.xml");
  }

You have to admin that this is pretty simple!

Ok so now for Wicket integration. I simply created a WicketHandler that can be added to the Jetty server. This handler takes a bean reference to the web application.

So first here is the web application:

<bean name="wicketWebApplication" class="com.sateh.test.webapp.HelloWorldApplication">
  </bean>

And here is the Jetty server:

<bean name="myJettyServer" class="com.sateh.spring.jetty.JettyServer">

    <property name="listeners">
      <list>
        <bean class="org.mortbay.http.SocketListener">
          <property name="port" value="8088"/>
        </bean>
      </list>
    </property>

    <property name="contexts">
      <list>
<bean name="myContext1" class="com.sateh.spring.jetty.JettyContext">
          <property name="contextPath" value="/"/>
          <property name="resourceBase" value="./webroot"/>
          <property name="handlers">
            <list>
              <bean class="com.sateh.spring.jetty.WicketHandler">
                <property name="path" value="/"/>
<property name="webApplication" ref="wicketWebApplication"/>
              </bean>
            </list>
          </property>
        </bean>
      </list>
    </property>

  </bean>

As you can see this is all pretty much The Spring Way. The HelloWorldApplication that is referenced in the WicketHandler is basically the standard Wicket example. It doesn't have to inherit from a special SpringWebApplication because of IoC.

This is the code for HelloWorldApplication object:

public class HelloWorldApplication extends WebApplication
{
    public HelloWorldApplication()
    {
        getPages().setHomePage(HelloWorld.class);
    }
}

And this is the code for the HelloWorld page object:

public class HelloWorld extends WebPage
{
    public HelloWorld()
    {
        add(new Label("message", "Hello World!"));
    }
}

Injecting your application with other beans from the Spring context works as usual. For example, here is a little Hello World service.

public interface HelloWorldService
{
    String getMessage();
}

public class HelloWorldServiceImpl implements HelloWorldService
{
    public String getMessage()
    {
        return "Hallo, wereld!";
    }
}

<bean name="dutchHelloWorldService" class="com.sateh.test.webapp.HelloWorldServiceImpl"/>

<bean name="wicketWebApplication" class="com.sateh.test.webapp.HelloWorldApplication">
    <property name="helloWorldService" ref="dutchHelloWorldService"/>
  </bean>

public class HelloWorld extends WebPage
{
    public HelloWorld()
    {
        add(new Label("message", "Hello World!"));
    }

    private HelloWorldService mHelloWorldService;

    public HelloWorldService getHelloWorldService()
    {
        return mHelloWorldService;
    }

public void setHelloWorldService(HelloWorldService helloWorldService)
    {
        mHelloWorldService = helloWorldService;
    }
}

public class HelloWorld extends WebPage
{
    public HelloWorld()
    {
String message = ((HelloWorldApplication) getApplication ()).getHelloWorldService().getMessage();
        add(new Label("message", message));
    }
}

That cast is a bit yukkie but can easily be solved by making an abstract HelloWorldPage that has getters for the injected services.

What do you think?

 S.



-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.
Get Certified Today * Register for a JBoss Training Course
Free Certification Exam for All Training Attendees Through End of 2005
Visit http://www.jboss.com/services/certification for more information
_______________________________________________
Wicket-user mailing list
Wicket-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-user

Reply via email to