<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>bdp</groupId> <artifactId>bdp</artifactId> <version>1.0.0</version> <packaging>war</packaging> <name>bdp</name> <description>Basic Data Platform</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.2.0.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.defonds.bdp.city.controller" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>bdp</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>bdpmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/bdpmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>bdpmvc</servlet-name> <url-pattern>*.json</url-pattern> <url-pattern>*.html</url-pattern> </servlet-mapping> </web-app>
/** * File Name:CityController.java * * Copyright Defonds Corporation 2015 * All Rights Reserved * */ package com.defonds.bdp.city.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; /** * * Project Name:bdp * Type Name:CityController * Type Description: * Author:Defonds * Create Date:2015-8-27 * @version * */ @Controller @RequestMapping("/city") public class CityController { @RequestMapping("/welcome") public ModelAndView helloWorld() { String message = "<br><div style='text-align:center;'>" + "<h3>********** Hello World, Spring MVC Tutorial</h3>This message is coming from CityController.java **********</div><br><br>"; return new ModelAndView("welcome", "message", message); } }
创建 /WebContent/index.jsp 和 /WebContent/WEB-INF/jsp/welcome.jsp 视图文件。
/WebContent/index.jsp:
<html> <head> <title>Spring MVC Tutorial Series by defonds.com</title> <style type="text/css"> body { background-image: url('http://img.blog.csdn.net/20150827184458936'); } </style> </head> <body> <br> <div style="text-align:center"> <h2> Hey You..!! This is your 1st Spring MCV Tutorial..<br> <br> </h2> <h3> <a href="city/welcome.html">Click here to See Welcome Message... </a>(to check Spring MVC Controller... @RequestMapping("/city/welcome")) </h3> </div> </body> </html>/WebContent/WEB-INF/jsp/welcome.jsp:
<html> <head> <title>Spring MVC Tutorial by Defonds - Hello World Spring MVC Example</title> <style type="text/css"> body { background-image: url('http://img.blog.csdn.net/20150827184458936'); } </style> </head> <body>${message} <br> <br> </body> </html>
零基础搭建 spring mvc 4 项目成功。
本文侧重讲 spring mvc 的相关配置,也就是 controller 层的相关功能演示,关于 spring ioc、orm 和 mybatis 的事务集成,请参考另一篇博客《零基础整合 spring 4(包括mvc、context、orm) + mybatis 3 示例》,该文就是在本文基础上进行进一步添加了 service 和 dao 层的扩展。