Contents

Configuring Spring's application context

Now that web.xml is in place we must create the application-context.xml file we are referring to from web.xml.

  • Add a file called application-context.xml to your application's resources folder and give it the following content.
<?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" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:security="http://www.springframework.org/schema/security"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.1.xsd 
    http://www.springframework.org/schema/task 
    http://www.springframework.org/schema/task/spring-task-3.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="net.tekstenuitleg" />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>


</beans>

As you can see, there are three things in application-context.xml. First, the <mvc:annotation-driven> tag, which tells Spring that we will be using annotations to determine what our controllers and request mappings are, instead of declaring them with XML.

Secondly, there is a compontent-scan tag. This tells Spring to look for MVC components (mainly controllers) in the net.tekstenuitleg package. This tag willl cause Spring to start looking for our classes that have the @Controller annotation in the net.tekstenuitleg package and its subpackages.

  • Change the package name in the context:component-scan tag to reflect your own package structure.

The next entry  is the viewResolver bean definition. This tells Spring to look for views (JSPs) in the /WEB-INF/views/ directory. 

  • In the webapp/WEB-INF directory create a views folder. This is where Spring will look for JSPs.

MVC project structure

Well now, we have done quite a bit of configuration on this page. On the next page we will do some basic MVC programming. We will create a controller and an accompanying view JSP to render some data.