Spring MVC 入门

作者:範宗雲 来源:原创 发布时间:2015-03-20 归档:springmvc

开发环境 : JDK 7 Maven 3 Tomcat 7 Spring 4.1.5 Eclipse Luna
项目依赖
			<properties>
			  <spring.version>4.1.5.RELEASE</spring.version>
			</properties>
			<dependencies>
			  <dependency>
			    <groupId>org.springframework</groupId>
			    <artifactId>spring-core</artifactId>
			    <version>${spring.version}</version>
			  </dependency>
			  <dependency>
			    <groupId>org.springframework</groupId>
			    <artifactId>spring-webmvc</artifactId>
			    <version>${spring.version}</version>
			  </dependency>
			</dependencies>
			
web.xml
			<servlet>
			  <servlet-name>mvc-dispatcher</servlet-name>
			  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
			  <load-on-startup>1</load-on-startup>
			</servlet>
			
			<servlet-mapping>
			  <servlet-name>mvc-dispatcher</servlet-name>
			  <url-pattern>/</url-pattern>
			</servlet-mapping>
			
			<context-param>
			   <param-name>contextConfigLocation</param-name>
			   <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
			</context-param>
			
			<listener>
			   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
			</listener>
			
在 web.xml 中需要配置 DispatcherServlet, 它用于处理符合规则的外部请求, 并将请求分发给合适的控制器来处理。
spring mvc 约定去 WEB-INF 下查找 [servlet-name]-servlet.xml ( /WEB-INF/mvc-dispatcher-servlet.xml ) 文件作为其配置文件。
除此之外, 还可以通过 init parameter 来指定 spring mvc 的配置文件, 具体如下:
			<servlet>
			  <servlet-name>mvc-dispatcher</servlet-name>
			  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
			  <init-param>
			    <param-name>contextConfigLocation</param-name>
			    <param-value>classpath:mvc-dispatcher.xml</param-value>
			  </init-param>
			  <load-on-startup>1</load-on-startup>
			</servlet>
			
			<servlet-mapping>
			  <servlet-name>mvc-dispatcher</servlet-name>
			  <url-pattern>/</url-pattern>
			</servlet-mapping>
			
mvc-dispatcher-servlet.xml
			<?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:mvc="http://www.springframework.org/schema/mvc"
			  xmlns:context="http://www.springframework.org/schema/context"
			  xsi:schemaLocation="http://www.springframework.org/schema/beans
			  http://www.springframework.org/schema/beans/spring-beans.xsd
			  http://www.springframework.org/schema/mvc
			  http://www.springframework.org/schema/mvc/spring-mvc.xsd
			  http://www.springframework.org/schema/context
			  http://www.springframework.org/schema/context/spring-context.xsd">
			  
			  <mvc:annotation-driven />
			  
			  <context:component-scan base-package="org.lychie.**.controller" />
			  
			  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			    <property name="prefix" value="/WEB-INF/pages/" />
			    <property name="suffix" value=".jsp" />
			  </bean>
			  
			</beans>
			
spring mvc 配置文件, InternalResourceViewResolver 用于支持 servlet 和 jsp 视图的解析。
视图的逻辑位置为:prefix + 逻辑名称 + suffix, 如逻辑名称为 hello, 则视图的逻辑位置为 /WEB-INF/pages/hello.jsp
Controller
			package org.lychie.springmvc.controller;
			
			import org.springframework.ui.ModelMap;
			import org.springframework.stereotype.Controller;
			import org.springframework.web.bind.annotation.RequestMapping;
			
			@Controller
			public class HelloWorldController {
			
				@RequestMapping("/hello")
				public String helloWorld(Model model){
					model.addAttribute("message", "Hello World!");
					return "hello";
				}
				
			}
@RequestMapping 用于将请求的 url 映射到相应的处理方法上。
访问方式 :http://localhost:8080/spring-mvc-helloworld/hello

示例代码下载
spring-mvc-helloworld.zip