Spring 4 MVC example with Maven
In this tutorial, we show you a Spring 4 MVC example, using Maven build tool.
Technologies used :
- Spring 4.3.0.RELEASE
- Maven 3
- JDK 1.8
- Eclipse Mars.2 Release (4.5.2)
- Boostrap 3
1. Project Structure
Download the project source code and review the project folder structure :
2. Maven
A 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:
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的更多相关文章
- Spring 4 MVC example with Maven - [Source Code Download]
In this tutorial, we show you a Spring 4 MVC example, using Maven build tool. Technologies used : Sp ...
- Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例
Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例 转自:通过注解的方式集成Spring 4 MVC+Hibernate 4+MySQL+Maven,开发项目样例 ...
- Spring+SpringMVC+MyBatis+LogBack+C3P0+Maven+Git小结(转)
摘要 出于兴趣,想要搭建一个自己的小站点,目前正在积极的准备环境,利用Spring+SpringMVC+MyBatis+LogBack+C3P0+Maven+Git,这里总结下最近遇到的一些问题及解决 ...
- 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 ...
- 菜鸟学习Spring Web MVC之二
有文章从结构上详细讲解了Spring Web MVC,我个菜鸟就不引据来讲了.说说强悍的XP环境如何配置运行环境~~ 最后我配好的环境Tomcat.Spring Tool Suites.Maven目前 ...
- Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介绍了普通方式开发spring web mvc web service.接下来看看使用spring b ...
- [转]Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
Spring Boot——2分钟构建spring web mvc REST风格HelloWorld http://projects.spring.io/spring-boot/ http://spri ...
- Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用
Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...
- Spring 4 MVC+Apache Tiles 3 Example
In this post we will integrate Apache Tiles 3 with Spring MVC 4, using annotation-based configuratio ...
随机推荐
- Mysq基础
本文是之前看博客时候的记录,忘记是哪位仁兄的了,在这只做一次转载: 常见误区 count(1)和count(primary_key) 优于 count(*) 很多人为了统计记录条数,就使用 count ...
- Weave Scope 容器地图 - 每天5分钟玩转 Docker 容器技术(80)
Weave Scope 的最大特点是会自动生成一张 Docker 容器地图,让我们能够直观地理解.监控和控制容器.千言万语不及一张图,先感受一下. 下面开始实践 Weave Scope. 安装 执行如 ...
- Spring Boot Starter 介绍
http://www.baeldung.com/spring-boot-starters 作者:baeldung 译者:http://oopsguy.com 1.概述 依赖管理是任何复杂项目的关键部分 ...
- 国内为什么没有好的 Stack Overflow 的模仿者?,因为素质太低?没有分享精神?
今天终于在下班前搞定一个技术问题,可以准时下班啦.当然又是通过StackOverflow找到的解决思路,所以下班路上和同事顺便聊起了它,两个资深老程序猿,还是有点感叹,中国的程序员群体人数应该不少,为 ...
- Java继承--子类的实例化过程
一个对象的实例化过程: Person p = new Person(); 1,JVM会读取指定的路径下的Person.class文件,并加载进内存,并会先加载Person的父类(如果有直接的父类的情况 ...
- java虚拟机指令dup的理解
举个例子: public class ExceptionTest{ void cantBeZero(int i) throws Exception{ throw new Exception(); } ...
- 【微信小程序开发】秒懂,架构及框架
今天1024程序员节,写文章庆祝!!! 今天的文章是讲微信小程序开发的,按理解把架构与框架说说.有不对之处请大神指点…… 微信小程序与web应用很像,但是原理不同,微信小程序是运行在微信应用内的,不是 ...
- 树莓派.安装Redis环境
先下载安装包 cd /home/pi/Downloadswget http://download.redis.io/releases/redis-4.0.1.tar.gz 解压 tar -zxf re ...
- 使用phpexcel类读写excel文件
使用原生php读写excel文件的博文地址: 基于使用原生php读写excel文件的不靠谱,本文将简单介绍如何使用第三方类库phpexcel来读写excel文件. 首先,需要到githut下载phpe ...
- scala攻略--简介
在个人学习scala的过程中,产生了写一系列随笔的想法,这些随笔包括:翻译自官网.其他英文网站的文章以及自己的心得体会,本文章作为这个系列中的第一个. 由于本人能力所限,以及对scala还处于初级阶段 ...