In this tutorial, we show you a Spring 4 MVC example, using Maven build tool.

Technologies used :

  1. Spring 4.3.0.RELEASE
  2. Maven 3
  3. JDK 1.8
  4. Eclipse Mars.2 Release (4.5.2)
  5. Boostrap 3

1. Project Structure

Download the project source code and review the project folder structure :

Spring 4 MVC example with Maven - [Source Code Download]

2. Maven

pom.xml template to quick start a Spring MVC project, it defines Spring 4 dependencies and Eclipse workspace configuration.

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 3 
 4     <modelVersion>4.0.0</modelVersion>
 5     <groupId>com.b510.hongten</groupId>
 6     <artifactId>springmvc</artifactId>
 7     <packaging>war</packaging>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <name>springmvc Maven Webapp</name>
10     <url>http://maven.apache.org</url>
11 
12 
13     <properties>
14         <project-name>springmvc</project-name>
15         <project-version>0.0.1-SNAPSHOT</project-version>
16         <java-version>1.8</java-version>
17         <org.springframework-version>4.3.0.RELEASE</org.springframework-version>
18     </properties>
19 
20 
21     <dependencies>
22         <!-- Spring MVC dependency -->
23         <dependency>
24             <groupId>org.springframework</groupId>
25             <artifactId>spring-webmvc</artifactId>
26             <version>${org.springframework-version}</version>
27         </dependency>
28         <dependency>
29             <groupId>javax.servlet</groupId>
30             <artifactId>javax.servlet-api</artifactId>
31             <version>3.0.1</version>
32             <scope>provided</scope>
33         </dependency>
34         <dependency>
35             <groupId>javax.servlet</groupId>
36             <artifactId>jstl</artifactId>
37             <version>1.2</version>
38         </dependency>
39 
40         <!-- log4j dependency-->
41         <dependency>
42             <groupId>log4j</groupId>
43             <artifactId>log4j</artifactId>
44             <version>1.2.17</version>
45         </dependency>
46 
47     </dependencies>
48 
49 
50     <build>
51         <finalName>${project-name}-${project-version}</finalName>
52         <pluginManagement>
53             <plugins>
54                 <plugin>
55                     <!-- Maven plugin -->
56                     <groupId>org.apache.maven.plugins</groupId>
57                     <artifactId>maven-compiler-plugin</artifactId>
58                     <version>2.3.2</version>
59                     <configuration>
60                         <source>${java-version}</source>
61                         <target>${java-version}</target>
62                     </configuration>
63                 </plugin>
64                 <plugin>
65                     <groupId>org.apache.maven.plugins</groupId>
66                     <artifactId>maven-war-plugin</artifactId>
67                     <version>2.4</version>
68                     <configuration>
69                         <warSourceDirectory>src/main/webapp</warSourceDirectory>
70                         <warName>${project-name}</warName>
71                         <failOnMissingWebXml>false</failOnMissingWebXml>
72                     </configuration>
73                 </plugin>
74             </plugins>
75         </pluginManagement>
76     </build>
77 </project>

3. Controller & Mapping

The @RequestMapping has been available since 2.5, but now enhanced to support REST style URLs.

 1 package com.b510.hongten.demo.web;
 2 
 3 import org.apache.log4j.Logger;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.ui.Model;
 6 import org.springframework.web.bind.annotation.PathVariable;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RequestMethod;
 9 
10 import com.b510.hongten.demo.entity.Demo;
11 
12 /**
13  * @author Hongten
14  * @date Nov 19, 2017
15  */
16 @Controller
17 @RequestMapping("/demo")
18 public class DemoController {
19 
20     private static final Logger logger = Logger.getLogger(DemoController.class);
21     
22     @RequestMapping(value = "/show/{name}", method = RequestMethod.GET)
23     public String show(@PathVariable("name") String name, Model model) {
24         logger.info("name is " + name);
25         Demo demo = new Demo();
26         demo.setTitle("Spring MVC Demo");
27         demo.setName(name);
28         
29         model.addAttribute(demo);
30         return "demo/show";
31     }
32 
33     @RequestMapping(value = "/show", method = RequestMethod.GET)
34     public String show(Model model) {
35         logger.info("show method...");
36         Demo demo = new Demo();
37         demo.setTitle("Spring MVC Demo");
38         demo.setName("Hongten");
39         model.addAttribute("demo", demo);
40         return "demo/show";
41     }
42 
43 }

4. JSP Views

A JSP page(show.jsp) to display the value, and include bootstrap css and js.

 1 <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
 2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 3 <!DOCTYPE html>
 4 <html lang="en">
 5 <jsp:include page="/WEB-INF/views/common/common-libs.jsp" />
 6 <head>
 7 <title>Spring MVC Demo</title>
 8 </head>
 9 
10 <nav class="navbar navbar-inverse navbar-fixed-top">
11   <div class="container">
12     <div class="navbar-header">
13         <a class="navbar-brand" href="#">Spring MVC Demo</a>
14     </div>
15   </div>
16 </nav>
17 
18 <div class="jumbotron">
19   <div class="container">
20     <h1>${demo.title}</h1>
21     <p>
22         <c:if test="${not empty demo.name}">
23             Hello <font color='red'>${demo.name}</font>
24         </c:if>
25 
26         <c:if test="${empty demo.name}">
27             Welcome Welcome!
28         </c:if>
29     </p>
30     <p>
31     <a class="btn btn-primary btn-lg" href="./demo/show" role="button">Home</a>
32     </p>
33   </div>
34 </div>
35 
36 <div class="container">
37 
38   <div class="row">
39     <div class="col-md-4">
40         <h2>Heading</h2>
41         <p>Totoro</p>
42         <p>
43         <a class="btn btn-default" href="./demo/show/Totoro" role="button">View details</a>
44         </p>
45     </div>
46     <div class="col-md-4">
47         <h2>Heading</h2>
48         <p>Tome James</p>
49         <p>
50         <a class="btn btn-default" href="./demo/show/Tome James" role="button">View details</a>
51         </p>
52     </div>
53     <div class="col-md-4">
54         <h2>Heading</h2>
55         <p>John Mohanmode</p>
56         <p>
57         <a class="btn btn-default" href="./demo/show/John Mohanmode" role="button">View details</a>
58         </p>
59     </div>
60   </div>
61 
62   <hr>
63   <footer>
64     <p>© Hongten 2017</p>
65   </footer>
66 </div>
67 </body>
68 </html>

5. Spring XML Configuration

5.1. Enable component scanning, view resolver and resource mapping.

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:context="http://www.springframework.org/schema/context"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 4     xsi:schemaLocation="
 5         http://www.springframework.org/schema/beans
 6         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 7         http://www.springframework.org/schema/mvc
 8         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
 9         http://www.springframework.org/schema/context
10         http://www.springframework.org/schema/context/spring-context-3.2.xsd">
11 
12     <!-- Enable component scanning -->
13     <context:component-scan base-package="com.b510.hongten" />
14 
15     <!-- Enable annotation -->
16     <mvc:annotation-driven />
17 
18     <!-- Enable view resolver -->
19     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
20         <property name="prefix">
21             <value>/WEB-INF/views/</value>
22         </property>
23         <property name="suffix">
24             <value>.jsp</value>
25         </property>
26     </bean>
27 
28     <!-- Enable resource mapping -->
29     <mvc:resources mapping="/resources/**" location="/resources/" />
30 
31 </beans>

5.2. Declares a DispatcherServlet in web.xml. If the Spring XML configuration file is NOT specified, Spring will look for the {servlet-name}-servlet.xml.

In this example, Spring will look for the spring-web-servlet.xml file.

 1 <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 3           http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 4     version="3.0">
 5 
 6     <servlet>
 7         <servlet-name>spring-web</servlet-name>
 8         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 9         <load-on-startup>1</load-on-startup>
10     </servlet>
11 
12     <servlet-mapping>
13         <servlet-name>spring-web</servlet-name>
14         <url-pattern>/</url-pattern>
15     </servlet-mapping>
16 
17 </web-app>

6. Build Project

6.1 Build the project in eclipse with maven

Right click project -> Run As -> Maven build...

Spring 4 MVC example with Maven - [Source Code Download]

6.2.Enter 'clean install' to build project.

clean install

Spring 4 MVC example with Maven - [Source Code Download]

6.3.Should see the 'BUILD SUCCESS' result.

Spring 4 MVC example with Maven - [Source Code Download]

7. Run Project on Tomcat Server

7.1.Select Servers tab and right click Tomcat v7.0 and select Add and Remove...

Spring 4 MVC example with Maven - [Source Code Download]

7.2.Add springmvc project to Tomcat.

Spring 4 MVC example with Maven - [Source Code Download]

7.3. Start Tomcat server.

Spring 4 MVC example with Maven - [Source Code Download]

 

8. Demo

The result like this:

http://localhost:8080/springmvc/

Spring 4 MVC example with Maven - [Source Code Download]

Click 'Home' button to go show page.

Spring 4 MVC example with Maven - [Source Code Download]

 

9. Source Code Download

The source code download below:

springmvc.zip

 

Good Luck!

========================================================

More reading,and english is important.

I'm Hongten

Spring 4 MVC example with Maven - [Source Code Download] 

大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。
Hongten博客排名在100名以内。粉丝过千。
Hongten出品,必是精品。

E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

========================================================

 

相关文章: