Building Spring MVC Framework Using IDEA

To be honest, I have working on this for at least two weeks. I read blogs, relevant books, and watched several related videos on various e-course. A large majority of them build Spring using MyEclipse which is out of my expectation. I can barely find any introduction for IDEA users, and even for those do, many are out-dated. God knows how many times I install and uninstall the framework. I wasted adding up to at least 24 hours to wait that damn Maven to configure the dependency. It drove me crazy especially when lots of homework amassed, meaning I have to work on those stuff at the midnight.

Since this is a very easy demo, and No Principle Explanation will be involved. This blog is solely used to remind me (or maybe you) how to build up a Spring MVC Framework using IDEA in a short time.

0x01 Tomcat Ready

Since it’s Spring MVC, what we’re building after all is a web application. Therefore, we first new an Java EE Project, calling it QuickDemo.



Click Finish button at the bottom, and a basic Java EE project is built up.

Now the project sidebar should look like this:


Don’t forget to edit configuration of Tomcat server!

Don’t forget to change the Application context to /, or later on all URL you visit will automatically add this QuickDemo_war_exploded beforehand.

0x02 Maven Dependencies Addition

​ Maven is a software project management and comprehension tool based on the concept of a project object model. In brief, it can be used to manage your various JARs imported from the external libraries.

​ Right click the project QuickDemo and choose to Add Framework Support:


Check the check-box of Maven.

groupID can be whatever you want(better be your company name).Mine is com.Michael.ArtifactId is your project name, here is QuickDemo. Don’t worry about everything else, just click Finish.

Waiting for several moments,you shall see sidebars become:


Open pom.xml, you shall see this:



Event Log may jump out saying that ‘Maven projects need to be imported’. Just click Import Changes is fine.

Next,we’re about to add dependencies of Spring JAR.

In pom.xml, write those content:

     <!--JUnit,used in test-->

        <!--Spring frameworks-->

            <!--Sometimes your servlet-api version may conflicts with default-->
            <!--scope means using maven ones-->

        <!--JSP View Resolver-->

        <!--finalName should be your project name-->
            <!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
                <!-- see -->

Import Changes, and done.

Just in case, in File>>Project Structure>>Artifacts, right-click and put these JARs into ROOT. Click Apply and then OK.


Now the sidebar should look like this:

All JARs are imported successfully.

0x03 Writing Configuration XML

​ The most frustrating work of all is writing the configuration XML. Right Click WEB-INF, and new an Spring Config XML,with the name of dispatcher-servlet.xml:


It should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""


Dispatcher-servlet plays an essential role in Spring MVC, it functions as a redirector, receiving requests and retrieving response and sending to other components.

​ But before we do anything, I suggest we first make some directories and packages in the src package.

​ Under src/main/java directory, new three independent packages, respectively named controllers, views,models.

​ Under WEB directory, you can new a new directory called JSP, putting your JSP that wrote beforehand under it. Mine JSP name is called index.jsp, and its content looks like this:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
  This is a quick demo.

Then, under controllers package, we can new a Java class with whatever name you want. According to the common naming principles, I’m calling it IndexController.

In, I wrote:

package controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

public class IndexController {
    @RequestMapping(value="/",method= RequestMethod.GET)
    public String index(){
        return "index";

Now the project structure should be like this:


After that, in dispatcher-servlet.xml, paste this:


    <context:component-scan base-package="controllers">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/JSP/"/>
        <property name="suffix" value=".jsp"/>
        <property name="exposeContextBeansAsAttributes" value="true"/>

And don’t forget web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""


        <!-- <async-supported>true</async-supported>-->


0x04 Run!

Ignoring all details such as ‘Why writing these codes’, we can click the Run button on the right-top corner of IDEA. There may be some trivial warnings, but as long as it’s not an error, let it go. Finally, at URL: http://localhost:8080 we should see:


It may seems easy, but for me with barely little knowledge of Spring at that time, writing XML is really a painful experience. I begin to miss the good days when writing Flask.


电子邮件地址不会被公开。 必填项已用*标注