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.

JavaEE1

JavaEE2

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

Now the project sidebar should look like this:

JavaEE3

Don’t forget to edit configuration of Tomcat server!
JavaEE4

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:

Maven1

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:

Maven2

Open pom.xml, you shall see this:

Maven3

Right?

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:

 <dependencies>
        <dependency>
     <!--JUnit,used in test-->
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <!--Spring frameworks-->
         <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>

        <!--Servlets-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <!--Sometimes your servlet-api version may conflicts with default-->
            <!--scope means using maven ones-->
            <scope>provided</scope>
        </dependency>

        <!--JSP View Resolver-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

    <build>
        <!--finalName should be your project name-->
        <finalName>QuickDemo</finalName>
        <pluginManagement>
            <!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

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.

Maven4

Now the sidebar should look like this:

Maven5
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:

Spring1

It should 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

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" %>
<html>
  <head>
    <title>Welcome!</title>
  </head>
  <body>
  This is a quick demo.
  </body>
</html>

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 IndexController.java, I wrote:

package controllers;

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

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

Now the project structure should be like this:

Spring2

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

  <context:annotation-config/>

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

    <mvc:annotation-driven/>
    <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"/>
    </bean>

And don’t forget web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <display-name>QuickDemo</display-name>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <!-- <async-supported>true</async-supported>-->
    </servlet>

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

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:

Run


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.

发表评论

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