环境:

系统:mac OS X El Capitan 10.11.1

Eclipse : Mars Release (4.5.0)

maven:apache-maven-3.2.3

jdk:1.7

spring版本:4.2.2.RELEASE

创建的project名称:springMVC-demo

过程

1、使用Eclipse构建maven的web项目

2、添加依赖

  • jsp-api
  • spring-core
  • spring-webmvc
  • jstl
  • standard
  • mysql-connector-java
<!-- 单元测试 -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
</dependency>
<!-- jsp-api的依赖,在配置到tomcat前,可以直接编译项目,scope设置为provided,不会被打包到war -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.0</version>
    <scope>provided</scope>
</dependency>
        <!-- Spring核心依赖 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
</dependency>
<!-- SpringMVC依赖 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
</dependency>
<!-- jstl相关依赖 -->
<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>
<!-- 数据库驱动 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>${mysql.version}</version>
</dependency>

3、配置web.xml

3.1 DispatcherServlet配置

<servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <!-- SpringMVC的配置文件位置,对应到src/main/resources -->
        <param-value>classpath*:/springMVC.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

3.2 编码过滤器encodingFilter配置

<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

4、SpringMVC配置

4.1在src/main/resource中创建spring-mvc.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: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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

</beans>

4.2 spring bean的自动扫描

<context:component-scan base-package="com.xxx.xxx"/>

让spring自动扫描com.xxx.xxx包下的 @Repository、@Component、@Service 和 @Constroller 注解,省去在XML中显式使用进行 Bean 的配置

com.xxx.xxx中应包含action、service和dao包,否则不能自动扫描,注解配置将无效

4.3 注解驱动

<mvc:annotation-driven/>

启动SpringMVC的注解驱动,主动扫描注解,代替配置文件

4.4 配置MappingJackson2HttpMessageConverter

<bean id="mappingJacksonHttpMessageConverter"
          class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="supportedMediaTypes">
        <list>
            <value>text/html;charset=UTF-8</value>
        </list>
    </property>
</bean>

避免IE执行AJAX时,返回JSON出现下载文件

此处需要ackson-databind的支持,故需要引入依赖

4.5 增加项目依赖

增加jackson-databind的依赖

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.0</version>
</dependency>

4.6 配置ViewResolver(一个返回视图用的bean)

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/pages/" />
    <property name="suffix" value=".jsp" />
</bean>

InternalResourceViewResolver:用于支持Servlet、JSP视图解析;

prefix和suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀),比如传进来的逻辑视图名为hello,则该该jsp视图页面应该存放在“pages/hello.jsp”;

4.7 配置静态文件映射

<mvc:resources mapping="/resources/**" location="/resources/"
                   cache-period="31556926"/>

将/resources/**的请求,映射到/resources对应的目录上

前端必须使用<c:url>标签进行访问才行。

5、创建Action类

package com.celloud.action;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * SpringMVC的action类示例,测试SpringMVC框架搭建是否成功
 * @author <a href="mailto:sunwendong@celloud.cn">sun8wd</a>
 * @date 2015年12月3日下午3:52:01
 * @version Revision: 1.0
 */
@Controller
@RequestMapping("/demo")
public class DemoAction {
    @RequestMapping("list")
    public ModelAndView index(String name){
        ModelAndView mv = new ModelAndView("demo");
        name=name==null?"world":name;
        System.out.println("name="+name);
        mv.addObject("name", name);
        return mv;
    }
}

6、创建jsp页面

在src/main/webapp下创建pages/demo.jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello world!</title>
</head>
<body>
    <h2>Hello ${name }!</h2>
</body>
</html>

7、启动

启动tomcat,访问:

http://localhost:8080/springMVC-demo/demo/list

页面显示Hello world!

http://localhost:8080/springMVC-demo/demo/list?name=sun8wd

页面显示Hello sun8wd!

8、SpringMVC项目搭建成功