((( 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 ttsetRepository()/tt method. The method will just store a reference to the repository in a field. The tthandleRequest/tt method is now basically the same as the ttdoPost/tt method from the servlet. Except, the JNDI code is now gone and the dependency (on the repository) is eminjected/em into the class (via the ttsetRepository()/tt method). Another change will be to store the values in the request object so they cn be rendered with a JSP. /p img style=clearboth; border solid'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.

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


About willCode4Beer