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 :

2. Maven

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

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.b510.hongten</groupId>
<artifactId>springmvc</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springmvc Maven Webapp</name>
<url>http://maven.apache.org</url> <properties>
<project-name>springmvc</project-name>
<project-version>0.0.1-SNAPSHOT</project-version>
<java-version>1.8</java-version>
<org.springframework-version>4.3.0.RELEASE</org.springframework-version>
</properties> <dependencies>
<!-- Spring MVC dependency -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <!-- log4j dependency-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency> </dependencies> <build>
<finalName>${project-name}-${project-version}</finalName>
<pluginManagement>
<plugins>
<plugin>
<!-- Maven plugin -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<warName>${project-name}</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

3. Controller & Mapping

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

 package com.b510.hongten.demo.web;

 import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import com.b510.hongten.demo.entity.Demo; /**
* @author Hongten
* @date Nov 19, 2017
*/
@Controller
@RequestMapping("/demo")
public class DemoController { private static final Logger logger = Logger.getLogger(DemoController.class); @RequestMapping(value = "/show/{name}", method = RequestMethod.GET)
public String show(@PathVariable("name") String name, Model model) {
logger.info("name is " + name);
Demo demo = new Demo();
demo.setTitle("Spring MVC Demo");
demo.setName(name); model.addAttribute(demo);
return "demo/show";
} @RequestMapping(value = "/show", method = RequestMethod.GET)
public String show(Model model) {
logger.info("show method...");
Demo demo = new Demo();
demo.setTitle("Spring MVC Demo");
demo.setName("Hongten");
model.addAttribute("demo", demo);
return "demo/show";
} }

4. JSP Views

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

 <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<jsp:include page="/WEB-INF/views/common/common-libs.jsp" />
<head>
<title>Spring MVC Demo</title>
</head> <nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">Spring MVC Demo</a>
</div>
</div>
</nav> <div class="jumbotron">
<div class="container">
<h1>${demo.title}</h1>
<p>
<c:if test="${not empty demo.name}">
Hello <font color='red'>${demo.name}</font>
</c:if> <c:if test="${empty demo.name}">
Welcome Welcome!
</c:if>
</p>
<p>
<a class="btn btn-primary btn-lg" href="./demo/show" role="button">Home</a>
</p>
</div>
</div> <div class="container"> <div class="row">
<div class="col-md-4">
<h2>Heading</h2>
<p>Totoro</p>
<p>
<a class="btn btn-default" href="./demo/show/Totoro" role="button">View details</a>
</p>
</div>
<div class="col-md-4">
<h2>Heading</h2>
<p>Tome James</p>
<p>
<a class="btn btn-default" href="./demo/show/Tome James" role="button">View details</a>
</p>
</div>
<div class="col-md-4">
<h2>Heading</h2>
<p>John Mohanmode</p>
<p>
<a class="btn btn-default" href="./demo/show/John Mohanmode" role="button">View details</a>
</p>
</div>
</div> <hr>
<footer>
<p>© Hongten 2017</p>
</footer>
</div>
</body>
</html>

5. Spring XML Configuration

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

 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!-- Enable component scanning -->
<context:component-scan base-package="com.b510.hongten" /> <!-- Enable annotation -->
<mvc:annotation-driven /> <!-- Enable view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean> <!-- Enable resource mapping -->
<mvc:resources mapping="/resources/**" location="/resources/" /> </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.

 <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"> <servlet>
<servlet-name>spring-web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>spring-web</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

6. Build Project

6.1 Build the project in eclipse with maven

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

6.2.Enter 'clean install' to build project.

clean install

6.3.Should see the 'BUILD SUCCESS' result.

7. Run Project on Tomcat Server

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

7.2.Add springmvc project to Tomcat.

7.3. Start Tomcat server.

8. Demo

The result like this:

http://localhost:8080/springmvc/

Click 'Home' button to go show page.

9. Source Code Download

The source code download below:

springmvc.zip

Good Luck!

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

More reading,and english is important.

I'm Hongten

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

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

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

Spring 4 MVC example with Maven - [Source Code Download]的更多相关文章

  1. Spring 4 MVC example with Maven

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

  2. Google Chrome 源码下载地址 (Google Chrome Source Code Download)

    1. Google Chrome 源码 SVN 地址:http://src.chromium.org/svn.包含有 Chrome.Gears.Webkit.GCC 等源码以及编译依赖工具.Chrom ...

  3. [解决]ASP.NET MVC 4/5 源码调试(source code debug)

    ========================ASP.NET MVC 4============================ ASP.NET MVC 4 source code download ...

  4. spring 3 mvc hello world + mavern +jetty

    Spring 3 MVC hello world example By mkyong | August 2, 2011 | Updated : June 15, 2015 In this tutori ...

  5. Spring 3 MVC and JSON example

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  6. Spring 3 MVC and JSR303 @Valid example

    http://www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/ ———————————————————————————— ...

  7. Spring 4 MVC+Apache Tiles 3 Example

    In this post we will integrate Apache Tiles 3 with Spring MVC 4, using annotation-based configuratio ...

  8. Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例

    Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例 转自:通过注解的方式集成Spring 4 MVC+Hibernate 4+MySQL+Maven,开发项目样例 ...

  9. Spring+SpringMVC+MyBatis+LogBack+C3P0+Maven+Git小结(转)

    摘要 出于兴趣,想要搭建一个自己的小站点,目前正在积极的准备环境,利用Spring+SpringMVC+MyBatis+LogBack+C3P0+Maven+Git,这里总结下最近遇到的一些问题及解决 ...

随机推荐

  1. base | AtomicIntegerT类

    1. 原子自增操作 type __sync_fetch_and_add (type *ptr, type value) 2. 原子比较和交换(设置)操作 type __sync_val_compare ...

  2. FATAL Fatal error during KafkaServerStable startup. Prepare to shutdown (kafka.server.KafkaServerStartable) java.io.FileNotFoundException: /tmp/kafka-logs/.lock (Permission denied)

    1.启动kafka的时候,报错如下所示: [-- ::,] INFO zookeeper state changed (SyncConnected) (org.I0Itec.zkclient.ZkCl ...

  3. Multidex(二)之Dex预加载优化

    Multidex(二)之Dex预加载优化 https://www.jianshu.com/p/2891599511ff

  4. MediatR

    1.MediatR是什么? 微软官方eshopOnContainer开源项目中使用到了该工具, mediatR 是一种中介工具,解耦了消息处理器和消息之间耦合的类库,支持跨平台 .net Standa ...

  5. JVM性能参数优化

    以上是jdk1.7的默认GC回收器 Java的gc回收的类型主要有几种 UseSerialGC, UseConcMarkSweepGC, UseParNewGC, UseParallelGC, Use ...

  6. 麒麟Kylin

    开源的分布式分析引擎,提供Hadoop/Spark之上的SQL查询接口及多维分析(MOLAP)能力以支持超大规模数据,能在亚秒内查询巨大的Hive表: Kylin的主要特点包括支持SQL接口.支持超大 ...

  7. bind注意事项(传引用参数的时候)

    默认情况下,bind的那些不是占位符的参数被拷贝到bind返回的可调用对象中. 当需要把对象传到bind中的参数中时,需要使用ref或者cref. 例如: #include<iostream&g ...

  8. Linux下C语言的文件操作

    代码: #include <stdio.h> #include <string.h> #include <fcntl.h> /*************基本的函数A ...

  9. css图片根据div宽高比例自适应

    1.div布局 <div class="card-img-show"> <div class="upload-img-conss" > ...

  10. Spark-Unit2-Spark交互式命令行与SparkWordCount

    一.Spark交互式命令行 启动脚本:spark-shell 先启动spark:./start-all.sh 本地模式启动命令:/bin/spark-shell 集群模式启动命令:/bin/spark ...