((( Draft )))

Getting Started with JCR (Content Repository for Java, JSR-170) Page: 3

Page 1 | Page 2 | Page 3 |Page 4 | Page 5

Enter Spring

Congratulations, for making it this far. Now, if you ask me, having to do all of the JNDI stuff in all of my servlets is going to get old, very quickly. Besides, it has nothing to do with the businss at hand. Let's bring in the Spring Framework to manage dependences (BTW, yes, this really is just a lead in to use Spring in a few more areas later).

First, let's create a controller class to take the place of the servlet. This time, instead of calling JNDI to get a reference to the repository, create a setRepository() method. The method will just store a reference to the repository in a field. The handleRequest method is now basically the same as the doPost method from the servlet. Except, the JNDI code is now gone and the dependency (on the repository) is injected into the class (via the setRepository() method). Another change will be to store the values in the request object so they cn be rendered with a JSP.

Hello World controller

Now, copy the following files into /WEB-INF/lib: spring.jar, spring-aop.jar, and spring-beans-2.0.jar. Next, define a servlet in the web.xml named jcrApp. Define its class as org.springframework.web.servlet.DispatcherServlet. Next, create an init parameter for it named contextConfigLocation with a value of /WEB-INF/jcrAppContext.xml. Load on startup? You better believe it, set it to 1.

Fragment of the web.xml describing the DispatcherServlet

The url pattern for the servlet is pretty restrictive. This allows up to keep it simple for now. Later we can broaden its reach. I know by now, you've guessed that since you defined a config file that doesn't exist; we're going to have to create it. Well, what are you waiting for? The code follows.

/WEB-INF/jcpAppContext.xml
The Spring context configuration file

The configuration file defines 3 beans to be assembled by the Spring Framework. The first bean demoRepository uses the JndiObjectFactoryBean class. It is able to stand-in for the Repository and present a reference to the Repository object retrievd from JNDI. The next bean is going to reference the controller that you just created. The property repository tells the Spring Framework to call the setRepository() method. The <ref bean="demoRepository" /> tells Spring what value to pass as a parameter to the method. In this case, its going to be the bean defined previously. The last bean urlMapping lets Spring map urls to controller classes in the DispatcherServlet.

So, the course of events are, when the server starts, Spring will lookup the repository, it will initalize the controller with the repository, and then configure the DispatcherServlet to call the controller when someone navigates to /jcrHello.htm. The controller, once finished working, will pass control to the JSP, helloControllerOut.jsp which will generate the HTML to send to the browser.

I almost forgot, the code for helloControllerOut.jsp follows.

JSP to render hello world results

Now, we are going to run the hello world one more time (last time, I promise) using Spring, to manage the configuration. So, now starting the server and pointing the browser to: http://127.0.0.1:8080/jcrWeb/jcrHello.htm should return the hello world text from the repository.

Hello World, from the spring configured servlet communicating with the repository

Whew! Now, that is finished, get a coffee, and take a break. The next few pages are going to take some work.


Feel free to send comments to: feedback@willcode4beer.com
Page 1 | Page 2 | Page 3 |Page 4 | Page 5


Sponsors:

About willCode4Beer