Setting up a spring MVC project with maven in eclipse

Create Dynamic Web Project as shown in Figure 7.1

Figure 7.1 Figure 7.1

Now enable Maven by right-clicking the project in the Project Explorer and selecting Configure – “Convert to Maven Project” (This is for the new m2e plugin. For the old m2eclipse plugin this is Maven – “Enable Dependency Management”) as shown in Figure 7.2

Figure 7.2 Figure 7.2

Enter all details as required and remember to make sure the packaging type to war as shown in Figure 7.3

Figure 7.3 Figure 7.3

Now write the following code in pom.xml to add Spring Dependency in project.

    <!-- Spring 3 dependencies -->

1. Create Controller

Now create HelloWorldController in com.kruders.controller package and write the following code.

package com.kruders.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
public class HelloWorldController {
    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Hello World!!!");
        return "helloworld";

2. Create View

Create jsp folder in WEB-INF and create helloworld.jsp file in jsp folder

Now write the following code in helloworld.jsp file


3. Configuration

Create Spring Bean Configuration in WEB-INF folder and name it dispatcher-servlet.xml and add the following code.

<context:component-scan base-package="com.kruders.controller" />
    <property name="prefix">
    <property name="suffix">

4. Integrate Spring in Web App

To integrate Spring in Web Application, write the following code in web.xml


5. Add Maven Dependency

Right-Click on project and Select Properties. Now select Deployment Assembly as shown in Figure 7.4

Figure 7.4 Figure 7.4

Now Click Add and Select Java Build Path Entries and Click Next as shown in Figure 7.5

Figure 7.5 Figure 7.5

Select Maven Dependencies and Click Finish as showing in Figure 7.6

Figure 7.6 Figure 7.6

6. Run

Now run the project as shown in Figure 7.7

Figure 7.7 Figure 7.7

The folder structure of the example is shown below in Figure 7.8

Figure 7.8 Figure 7.8

You can download the source code of this example here.

4 Responses to Setting up a spring MVC project with maven in eclipse

  1. Roman June 17, 2013 at 3:35 pm #

    Could you please provide the full text of Spring Bean Configuration file dispatcher-servlet.xml. I got an error in that file “The prefix “context” for element “context:component-scan” is not bound” for line “”. Thanks

Leave a Reply