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="" xmlns:xsi=""
    xmlns:context="" xmlns:util=""
    xmlns:tx="" xmlns:security=""
    xmlns:p="" xmlns:task="" 

    <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" />


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.