JHU Object-Oriented Software Engineering Course Homepage
Deploy the Enterprise Application
Table of Contents

Creating a Simple Page


8. Configure Spring

Next, we configure Spring. Spring serves two purposes in our web application. The Spring core is used to wire or connect different pieces of our web application together; this allows the application logic to act as independent from the presentation logic without either side needing to know about the implementation of the other. This is often termed inversion of control or dependency injection.

Spring MVC is another component of the Spring framework; its purpose is to provide a model-view-controller design for the web application's presentation tier. Spring MVC will be responsible for figuring out which controllers are responsible for managing a given web request as well as how the response from the web application will be constructed.

We start by configuring the web.xml file in the WebContent/WEB-INF directory of the web project. If you do not have a web.xml in that location, you will need to create one; otherwise, edit its contents to look like the following.

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>testWeb</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>test</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>test</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/test-servlet.xml</param-value> </context-param> </web-app>

We can break down the contents of this file as follows:

At this point, we need to provide Spring with a configuration file as mentioned in the web.xml. Create a file named test-servlet.xml in the WEB-INF directory with the following contents.

<?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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <tx:annotation-driven /> <context:component-scan base-package="edu.jhu.cs.oose.j2ee.example" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost/j2ee" /> <property name="username" value="j2ee" /> <property name="password" value="j2ee" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="edu.jhu.cs.oose.j2ee.example.**.*" /> <property name="hibernateProperties"> <props> <prop key="hibernate.hbm2ddl.auto">create</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/pages/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>

This Spring configuration file will see us through the rest of the process and will permit us to configure our application using Java annotations (rather than going back to the XML file all the time). There are a few points of interest to highlight:

Once you are finished, the application should look like the following.

The server should start with this configuration; you should start the web application and ensure that there are no exceptions. It will not, however, present any additional functionality yet. Once you have performed this sanity check, stop the server and continue with the next step.

Warning: JBoss 7.01 does not function correctly at this point; the application will raise a ClassNotFoundException looking for javax.servlet.ServletContextListener.

Deploy the Enterprise Application
Table of Contents

Creating a Simple Page

Written by Zachary Palmer with help from Varun Sharma. Corrections and suggestions are welcome; please e-mail zachary dot palmer xX att Xx jhu.edu