For a school project, I had to use the Spring MVC Framework, and I found it an absolute nightmare to get it to work. For first thoughts, I am not very familiar with tools like Maven (and even the Intellij IDE), so it naturally took longer. Additionally, most of the tutorials I followed on the internet did not work for me, and I was running into some or the other issues.

This post is a sort of self documentation, so that I know the steps I need to follow, to get up and running with Spring MVC with minimal configuration. I document two methods to get Spring MVC to work with Intellij. The first one uses the Spring MVC template that is there in Intellij, whereas the second one uses Maven. As I don’t have that much experience with using Maven, and Spring, I probably can’t recommend one method over another. I am using Intellij IDEA 2016.3 (Ultimate Edition). This could be downloaded at Jetbrains (The Ultimate edition has a free trial for 30 days, though you could get a licence with a University ID).

Using Spring MVC Template:

1.) Create a new project, and select the Spring template, with the Spring MVC option checked. Intellij will create a new project for you

with_spring

2.) In the web/WEB-INF/web.xml file, change the following mapping

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.form</url-pattern>
</servlet-mapping>

to

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

3.) Go to Project Structure, and select the Problems section. There should be 2 problems that you need to fix. Fix both problems, and click Apply to persist the changes.

problems

  • Add Spring-4.3.6-RELEASE to artifact
  • Add Spring MVC-4.3.6-RELEASE to artifact

4.) Create a controller class in src. I have created a class called ‘IndexController’.

package org.saurabhsood.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * Created by saurabh on 2/11/17.
 */

@Controller
public class IndexController {
    @GetMapping("/")
    public String index(Model m) {
        m.addAttribute("someAttribute", "someValue");
        return "index";
    }
}

5.) Edit the web/WEB-INF/dispatcher-servlet.xml file, and make it 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"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<mvc:annotation-driven />
<context:component-scan base-package=”org.saurabhsood.controllers” />

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

 

6.) Create a directory called views in WEB-INF and move index.jsp there. Edit index.jsp, and add the following code, to display the variable we added to the Model

<p>${someAttribute}</p>

7.) Run -> Edit Configurations-> Add Local Tomcat Server. I already had Tomcat in /opt/tomcat, and just pointed Intellij to that directory. Give it a name like Tomcat.

tomcat

8.) Look at the Warnings section. There will be some warnings. Click on Fix to fix the warnings. Click on Apply.

9.) Finally, Run the application. If you did everything correctly, you should have the model value shown on the JSP page.

output-spring

With Maven

1.) Create New Project -> Maven Project. Do not select an archetype right now. In the next screen, add an artifact ID and Group ID

1 2-create

2.) When the project gets created, the pom.xml file gets opened. Intellij also gives a message saying ‘Maven projects need to be imported’. Select ‘Enable Auto-Import’.

3.) Add the following to the pom.xml file inside the <project> tag:

<packaging>war</packaging>

<properties>
<spring.version>4.3.0.RELEASE</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>

Maven will auto import the modules for you

4.) Create a webapp/WEB-INF directory inside ‘main’
5.) Create a web.xml file in WEB-INF

6.) Add the following code to the web.xml file:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<web-app xmlns=”http://java.sun.com/xml/ns/j2ee&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&#8221;
version=”2.4″>

<display-name>HelloWorld Application</display-name>
<description>
This is a simple web application with a source code organization
based on the recommendations of the Application Developer’s Guide.
</description>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

7.) In WEB-INF, create a file called dispatcher-servlet.xml. Add the following code


<?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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<mvc:annotation-driven />
<context:component-scan base-package=”org.saurabhsood.controllers” />

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

</beans>

Notice the base-package attribute for context. You should change this to the package that contains your controllers (which you create in the next step)

8.) Create the package which you specified in the dispatcher-servlet.xml. I created a class called IndexController. I had the following code:

package org.saurabhsood.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.ui.Model;

/**
 * Created by saurabh on 2/10/17.
 */

@Controller
public class IndexController {

    @GetMapping("/")
    public String index(Model m) {
        m.addAttribute("someAttribute", "someValue");
        return "index";
    }
}

9.) Create a directory called views in WEB-INF. In that directory, add a file called index.jsp. Add the following line in <body>, to display the model attribute:

<p>${someAttribute}</p>

10.) Select Edit Run Configurations -> Tomcat -> Local.

11.) Look at the warnings. Click on Fix, and select the option with ‘war-exploded’ when asked to select the artifact to deploy.

3-fix-problems

12.) Run the application. Hopefully you should get the output of the model information.

output-maven

 

Hopefully this long post helps kick start the development, and spares the pain that I had to go through over the period of 4 days 😉

Advertisements