一.环境配置:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.1.5.RELEASE</version>
    </dependency>

二.web.xml配置

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >
<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"
         version="3.0">
  <display-name>Archetype Created Web Application</display-name>
  <welcome-file-list>
    <welcome-file>/index.jsp</welcome-file>
  </welcome-file-list>
  <!-- Spring MVC配置 -->
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--Spring-servlet.xml config-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-servlet.xml</param-value>
    </init-param>
    <!-- load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法) -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>CharacterEncodingFilter</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>forceRequestEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>forceResponseEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <url-pattern>/</url-pattern>
  </filter-mapping>
</web-app>

 

<servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

上述配置说明将拦截除了jsp文件的所有文件,若想加载其他的本地文件,则在springmvc.xml配置文件中加上如下注解

<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>

 表示为加载main文件下的js文件中的文件以及所有子文件中的文件

三.springmvc.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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">


    <!-- 启动包扫描功能,以便注册带有@Controllers、@service、@repository、@Component等注解的类成为spring的bean -->
    <context:component-scan base-package="main.controllers">
        <!--&lt;!&ndash;只扫描控制器&ndash;&gt;-->
        <!--<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>-->
    </context:component-scan>
    <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/"/>    <!-- 前缀 -->
        <property name="suffix" value=".jsp"/>    <!-- 后缀 -->
    </bean>
    <!--把mvc不能处理的请求交给tomcat-->
    <mvc:default-servlet-handler/>
    <!--能支持mvc的一些更高级的功能-->
    <mvc:annotation-driven/>
    <!-- 访问静态文件(jpg,js,css)的方法 -->
    <!--<mvc:resources location="/files/" mapping="/files/**" />-->
    <!--<mvc:resources location="/scripts/" mapping="/scripts/**" />-->
    <!--<mvc:resources location="/styles/" mapping="/styles/**" />-->
    <!--<mvc:resources location="/Views/" mapping="/Views/**" />-->
</beans>

四.基本数据类型和对象类型做参数

1.通过@RequestMapping注解设置方法的访问路径

2.可直接在方法的参数列表中加入参数,springmvc自动根据对应的变量进行传参,当参数为对象时,则springmvc将根据创建的对象类的属性进行一一传参

五.@ResponseBody

springmvc管理的方法一般都要加上return表示跳转的路径,当方法返回值为void时,则表示在本页面输出,@responsebody注解底层由jackson实现,则表示在本页面输出,然后返回值是一个json值