---
title: "Spring mvc简单配置"
author: "goodsense"
date: "2013-06-19T16:20:11Z"
tags: ["spring mvc", "配置"]
categories: ["编程语言"]
slug: "spring-mvc"
draft: false
lastmod: "2013-06-19T16:20:11Z"
id: 1889281
url: "https://goodsense.iteye.com/blog/1889281"
---

Spring MVC做为SpringFrameWork的后续产品,Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构

Spring 的 Web MVC 框架是围绕 `DispatcherServlet` 设计的，它把请求分派给处理程序，同时带有可配置的处理程序映射、视图解析、本地语言、主题解析以及上载文件支持。

 

下面看一下Spring mvc 的简单配置入门:

首先新建一个Web项目,引入Spring相关jar包,这里在我们可以忽略spring与Struts2集成的jar包,引入Spring mvc相关jar包.相关jar包如下:这里需要注意,需要引入servlet-api.jar包

   
![](http://dl2.iteye.com/upload/attachment/0085/6977/afab5336-307a-3605-8829-a2472be3712f.gif)  
 

DispatcherServlet是spring mvc 的一个前端控制器，当一个请求过来时转给DispatcherServlet处理

首先我们在web.xml中声明DispatcherServlet处理请求:

Xml代码  [![收藏代码](http://mylfd.iteye.com/images/icon_star.png)](http://mylfd.iteye.com/blog/1886061 "收藏这段代码")

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;web-app version="2.5"
03. xmlns="http://java.sun.com/xml/ns/javaee"
04. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
06. http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd"&gt;
07. &lt;servlet&gt;
08. &lt;servlet-name&gt;springMVC&lt;/servlet-name&gt;
09. &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
10. &lt;!-- 设置servlet的启动级别 --&gt;
11. &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
12. &lt;/servlet&gt;
13. &lt;servlet-mapping&gt;
14. &lt;servlet-name&gt;springMVC&lt;/servlet-name&gt;
15. &lt;url-pattern&gt;/&lt;/url-pattern&gt;
16. &lt;/servlet-mapping&gt;
17. &lt;/web-app&gt;

 

DispatcherServlet在初始化的过程中需要一个配置文件来产生文件中的各种Bean,默认情况下,配置文件是放在WEB-INF下,其命名是有约定的,是我们在web.xml中声明的DispatcherServlet名称-servlet.xml ,本例中其名称为:springMVC-servlet.xml :

Xml代码  [![收藏代码](http://mylfd.iteye.com/images/icon_star.png)](http://mylfd.iteye.com/blog/1886061 "收藏这段代码")

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
04. xmlns:context="http://www.springframework.org/schema/context"
05. xmlns:mvc="http://www.springframework.org/schema/mvc"
06. xsi:schemaLocation="
07. http://www.springframework.org/schema/beans
08. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
09. http://www.springframework.org/schema/context
10. http://www.springframework.org/schema/context/spring-context-3.0.xsd
11. http://www.springframework.org/schema/mvc
12. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"&gt;
13. &lt;bean
14. class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
15. &lt;property name="prefix" value="/" /&gt;
16. &lt;property name="suffix" value=".jsp" /&gt;
17. &lt;/bean&gt;
18. &lt;/beans&gt;

Controller处理完成后会交给InternalResourceViewResolver处理,InternalResourceViewResolver用于支持Servlet、JSP视图解析.属性名为prefix的作用是指定视图文件的位置,属性名为suffix是作用是视图文件的类型,例如当我们prefix指定为:/mvc时 ,当传进来的逻辑视图名为hello它就会找mvc/hello.jsp.

 

然后我们新建一个控制器

 

 

Xml代码  [![收藏代码](http://mylfd.iteye.com/images/icon_star.png)](http://mylfd.iteye.com/blog/1886061 "收藏这段代码")

01. package gd.hz.springmvc.controller;
02. import javax.servlet.http.HttpServletRequest;
03. import javax.servlet.http.HttpServletResponse;
04. import org.springframework.web.servlet.ModelAndView;
05. import org.springframework.web.servlet.mvc.Controller;
06. public class HelloWorld implements Controller {
07. public ModelAndView handleRequest(HttpServletRequest request ,
08. HttpServletResponse response) throws Exception {
09. return new ModelAndView("hello");
10. }
11. }

 

控制器必须实现Controller接口,注意不选错了Controller,注意一下包全称.后面我还会讲其它的处理方法.

方法handleRequest,处理过来的请求,并转到相关页面.new ModelAndView("hello")返回逻辑视图名称为hello,所以根据上面的配置,它会到WebRoot下找hello.jsp页面.

 

新建hello.jsp页面

Html代码  [![收藏代码](http://mylfd.iteye.com/images/icon_star.png)](http://mylfd.iteye.com/blog/1886061 "收藏这段代码")

01. &lt;%@ page language="java" contentType="text/html; charset=UTF-8"
02. pageEncoding="UTF-8"%&gt;
03. &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
04. &lt;html&gt;
05. &lt;head&gt;
06. &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
07. &lt;title&gt;Hello SpringMVC&lt;/title&gt;
08. &lt;/head&gt;
09. &lt;body&gt;
10. 你好:Spring MVC
11. &lt;/body&gt;
12. &lt;/html&gt;

 

在springMVC-servlet.xml声明视图访问路径:

Xml代码  [![收藏代码](http://mylfd.iteye.com/images/icon_star.png)](http://mylfd.iteye.com/blog/1886061 "收藏这段代码")

1. &lt;bean name="/mvc/hello" class="gd.hz.springmvc.controller.HelloWorld"&gt;&lt;/bean&gt;

 

 

输入url进行测试:http://localhost/SpringHelloWorld/mvc/hello