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).
I deploy this with: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>
You have to admin that this is pretty simple!Code:public static void main(String[] args) { new ClassPathXmlApplicationContext("spring-context.xml"); }
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:
And here is the Jetty server:Code:<bean name="wicketWebApplication" class="com.sateh.test.webapp.HelloWorldApplication"> </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.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>
This is the code for HelloWorldApplication object:
And this is the code for the HelloWorld page object:Code:public class HelloWorldApplication extends WebApplication { public HelloWorldApplication() { getPages().setHomePage(HelloWorld.class); } }
Injecting your application with other beans from the Spring context works as usual. For example, here is a little Hello World service.Code:public class HelloWorld extends WebPage { public HelloWorld() { add(new Label("message", "Hello World!")); } }
Code:public interface HelloWorldService { String getMessage(); } public class HelloWorldServiceImpl implements HelloWorldService { public String getMessage() { return "Hallo, wereld!"; } }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>That cast is a bit yukkie but can easily be solved by making an abstract HelloWorldPage that has getters for the injected services.Code:public class HelloWorld extends WebPage { public HelloWorld() { String message = ((HelloWorldApplication) getApplication()).getHelloWorldService().getMessage(); add(new Label("message", message)); } }
What do you think?
S.


Reply With Quote