In part 4 of this series, we’ll finish off the configuration for the web application in Eclipse with Spring and Log4J configuration files.  We’ll also start the Tomcat server with Sysdeo and our dependencies loaded for the first time, and verify that Spring is working correctly.

Be sure you have set up Sysdeo Tomcat plugin and have your workspace setup as described if you want to follow along with the examples.  If you missed the previous parts, you can find them here: part 1, part 2, part 3.

Starting the Server

Before we start the Tomcat server for the first time with our dependencies in place, we need to add a couple of files that are referenced in our web.xml file.  The first file is log4j.properties, the configuration file for Log4J, which is referenced in our web.xml file.

This file goes in /src/main/webapp/WEB-INF/log4j.properties, and it should look something like this:

log4j.rootLogger=ALL, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyMMdd HH:mm:ss}|%-5p|%40.40c|%X{user}|%m%n

log4j.logger.org.apache=INFO
log4j.logger.org.springframework=INFO

This is a fairly standard log4j configuration that routes all messages to the console.

The second is dispatch-servlet.xml, which (for this project) is both the web and application Spring context configuration file.  The file goes into /src/main/resources/spring/dispatch-servlet.xml and should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans    xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        ">
        

    <context:annotation-config />
    
    <context:component-scan base-package="com.technologicaloddity.hellowebapp"/>
    
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass">
            <value>org.springframework.web.servlet.view.JstlView</value>
        </property>
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>            
        
</beans>

Let’s go over the dispatch-servlet.xml line by line.

Line 1-7: This is the Spring configuration schema declaration.  We are using two schema namespaces: "beans" and "context".

Line 9: This tells Spring that we are planning on using Annotations to configure some items.

Line 11: This tells Spring to look for annotations, starting at the base package of "com.technologicaloddity.hellowebapp".

Line 13-24: This is a normal Spring view resolver.  Basically it says that if a view is named "foo", Spring should look in /WEB-INF/jsp/foo.jsp for the actual view.

That’s it.  If you are used to Spring 2.0 configuration, you might be surprised that the configuration is so small.  That’s the power of Spring Annotations as we shall see later.

Now, it is time to start Sysdeo/Tomcat with all of our dependencies.  (Make sure you have done the steps in Part 1 first!)

Click on the Tomcat Start icon. Start-Tomcat-Button

If all goes well, you should see Tomcat start in your Eclipse console.  You should also see the DevLoader initialize and finally the Spring context load.  In particular, you should see these lines (they may look a bit different due to directory structures, that’s OK):

[DevLoader] Starting DevLoader

This is the DevLoader starting up.  Past that you should see it loading the dependency JAR files.   For example, here is the Spring WebMVC file being loaded:

[DevLoader] added file:/C:/Users/Bob/.m2/repository/org/springframework/spring-webmvc/3.0.0.RELEASE/spring-webmvc-3.0.0.RELEASE.jar

You should also see the Spring Framework initialize:

Initializing Spring root WebApplicationContext

If you have problems, be sure to check your Project > Preferences > Tomcat settings for the HelloWebApp project.

Go ahead to stop your Tomcat server for now.  Either click on the "Stop" red square icon in the Console window (this is normally what I do), or click the "Stop Tomcat" button on the toolbar. Stop-Tomcat-Button

That’s it for the configuration.  It may seem a bit tedious to setup, but once you have everything installed in Eclipse, and you know which files you will need, you can quickly set up a new web application project in 10-15 minutes.

In part 5, we get down to the easy part: writing some Java with Spring Annotations!

Leave a Reply

Your email address will not be published. Required fields are marked *