Announcement Announcement Module
Collapse
No announcement yet.
My take on Spring/Wicket integration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • My take on Spring/Wicket integration

    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).

    Code:
      <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:

    Code:
      public static void main&#40;String&#91;&#93; args&#41;
      &#123;
         new ClassPathXmlApplicationContext&#40;"spring-context.xml"&#41;;
      &#125;
    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:

    Code:
      <bean name="wicketWebApplication" class="com.sateh.test.webapp.HelloWorldApplication">
      </bean>
    And here is the Jetty server:

    Code:
      <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:

    Code:
    public class HelloWorldApplication extends WebApplication
    &#123;
        public HelloWorldApplication&#40;&#41;
        &#123;
            getPages&#40;&#41;.setHomePage&#40;HelloWorld.class&#41;;
        &#125;
    &#125;
    And this is the code for the HelloWorld page object:

    Code:
    public class HelloWorld extends WebPage
    &#123;
        public HelloWorld&#40;&#41;
        &#123;
            add&#40;new Label&#40;"message", "Hello World!"&#41;&#41;;
        &#125;
    &#125;
    Injecting your application with other beans from the Spring context works as usual. For example, here is a little Hello World service.

    Code:
    public interface HelloWorldService
    &#123;
        String getMessage&#40;&#41;;
    &#125;
    
    public class HelloWorldServiceImpl implements HelloWorldService
    &#123;
        public String getMessage&#40;&#41;
        &#123;
            return "Hallo, wereld!";
        &#125;
    &#125;
    Code:
      <bean name="dutchHelloWorldService" class="com.sateh.test.webapp.HelloWorldServiceImpl"/>
    
      <bean name="wicketWebApplication" class="com.sateh.test.webapp.HelloWorldApplication">
        <property name="helloWorldService" ref="dutchHelloWorldService"/>
      </bean>
    Code:
    public class HelloWorld extends WebPage
    &#123;
        public HelloWorld&#40;&#41;
        &#123;
            String message = &#40;&#40;HelloWorldApplication&#41; getApplication&#40;&#41;&#41;.getHelloWorldService&#40;&#41;.getMessage&#40;&#41;;
            add&#40;new Label&#40;"message", message&#41;&#41;;
        &#125;
    &#125;
    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.

  • #2
    Hello,
    I am wondering if you have posted any more sample code about this. I am trying simply to embed jetty in spring and get full control over it..

    I would love to see some sample code as to how this is done. When I do google searches your posts keep coming up but I think im missing some key sample code ... i think just seeing the skeleton of com.sateh.spring.jetty.JettyServer would be great !

    Maybe you have blogged this or something ?

    Thanks
    -Alan

    Comment

    Working...
    X