使用XML的配置的方式来演示Spring MVC 简单的例子,使用的技术:
1.Spring version 4.3.3.RELEASE
2.Maven 3
3.JDK 1.7
4.Eclipse 4.4
5.Boostrap 3
一,项目结构
二,详细代码
1.pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| <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.devnp</groupId> <artifactId>Spring4MVCMavenHelloWorld</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version>
<properties> <spring.version>4.3.3.RELEASE</spring.version> <jstl.version>1.2</jstl.version> <jdk.version>1.7</jdk.version> <servletapi.version>2.5</servletapi.version> <project.name>Spring4MVCMavenHelloWorld</project.name> </properties>
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servletapi.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.2.11.v20150529</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <webApp> <contextPath>/${project.name}</contextPath> </webApp> <httpConnector> <port>9999</port> </httpConnector> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version> <configuration> <downloadSources>true</downloadSources> <downloadJavadocs>true</downloadJavadocs> <wtpversion>2.0</wtpversion> <wtpContextName>${project.name}</wtpContextName> </configuration> </plugin> </plugins> <finalName>${project.name}</finalName> </build> </project>
|
2.web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <?xml version="1.0" encoding="UTF-8"?> <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_2_5.xsd" version="2.5">
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-web-servlet.xml</param-value> </context-param> <servlet> <servlet-name>spring-web</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value></param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
<servlet-mapping> <servlet-name>spring-web</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<display-name>Archetype Created Web Application</display-name>
</web-app>
|
3.spring-web-servlet.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<context:component-scan base-package="com.devnp.web" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/views/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
</beans>
|
4. HelloController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package com.devnp.web.controller;
import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView;
@Controller public class HelloController {
@RequestMapping(value = "/", method = RequestMethod.GET) public String printHello(ModelMap model){ model.addAttribute("title", "Spring 4 MVC Maven Hello World"); return "index"; } @RequestMapping(value = "/hello/{name:.+}", method = RequestMethod.GET) public ModelAndView hello(@PathVariable("name") String name) {
ModelAndView model = new ModelAndView(); model.setViewName("index"); model.addObject("title", "Welcome " + name);
return model;
} }
|
5. index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title>
<spring:url value="/resources/css/bootstrap.min.css" var="bootstrapCss" /> <link href="${bootstrapCss}" rel="stylesheet" />
</head> <body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header"> <a class="navbar-brand" href="#">${title}</a> </div>
</div>
</nav>
<div class="jumbotron">
<div class="container">
<c:if test="${not empty name}"> Hello ${name} </c:if>
<c:if test="${empty name}"> Welcome ! </c:if>
<a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-4">
<h2>Heading</h2>
ABC
<a class="btn btn-default" href="#" role="button">View details</a>
</div>
<div class="col-md-4">
<h2>Heading</h2>
ABC
<a class="btn btn-default" href="#" role="button">View details</a>
</div>
<div class="col-md-4">
<h2>Heading</h2>
ABC
<a class="btn btn-default" href="#" role="button">View details</a>
</div>
</div>
<hr>
<footer>
© devnp.com 2016
</footer>
</div>
<spring:url value="/resources/js/bootstrap.min.js" var="bootstrapJs" /> <spring:url value="/resources/js/jquery-1.12.4.min.js" var="jquery" /> <script src="${bootstrapJs}"></script> <script src="${jquery}"></script> </body> </html>
|
三,测试
可以选择tomcat或者其他的容器来测试,这里选择jetty.
jetty:run
访问:http://localhost:9999/Spring4MVCMavenHelloWorld/
访问:http://localhost:9999/Spring4MVCMavenHelloWorld/hello/duliu
四,代码下载
Spring4MVCMavenHelloWorld.zip
Author:
Darren Du
License:
Copyright (c) 2019 MIT LICENSE