SpringMVC开发环境搭建

1. 依赖包:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>5.1.7.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.1.7.RELEASE</version>
</dependency>

2. SpringMVC的配置文件:

  在“src\main\resources”源代码目录下创建“spring/application.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:mvn="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
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 包扫描 -->
    <context:component-scan base-package="cn.ll.action" />
    <!-- 启用控制层注解 -->
    <mvn:annotation-driven />
    <!--使用Dispatcher处理请求-->
    <mvn:default-servlet-handler />
    <!-- 定义页面资源解析处理类,匹配路径的前缀与后缀-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <!-- 为保存在WEB-INF静态资源设置映射访问路径-->
    <mvn:resources mapping="/js/**" location="/WEB-INF/js/" />
    <mvn:resources mapping="/css/**" location="/WEB-INF/css/" />
    <mvn:resources mapping="/images/**" location="/WEB-INF/images/" />
    <!-- 进行要加载的"*.properties"配置文件路径加载,直接通过CLASSPATH加载 -->
    <bean class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <array>
                <value>cn.ll.redis.message.pages</value>
                <value>cn.ll.redis.message.message</value>
            </array>
        </property>
    </bean>
    <!-- 配置上传文件解析处理类 -->
    <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置整体上传文件的最大数据量,本次允许上传最大文件量为5M -->
        <property name="maxUploadSize" value="5242880"/>
        <!-- 设置单个上传文件的最大数据量,本次允许上传最大文件量为2M -->
        <property name="maxUploadSizePerFile" value="253952"/>
        <!-- 允许占用的最大内存量,本次设置为10M -->
        <property name="maxInMemorySize" value="10485760"/>
        <!-- 设置上传文件的临时保存目录,该目录的内容在每一次请求之后都需要清空 -->
        <property name="uploadTempDir" value="/tmp"/>
    </bean>
    <!-- 配置全局跳转页面 -->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" >
        <property name="exceptionMappings"><!-- 异常映射 -->
            <props>
                <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">plugins/errors</prop>
            </props>
        </property>
    </bean>
</beans>

3. 配置web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

  <!-- 1、SpringMVC本身是运行在Spring容器之中,所以需要定义一个Spring容器的基本配置文件路径 -->
  <context-param> <!-- 配置全局的初始化参数,这个参数依靠ServletContext.getInitParameter()获取 -->
    <param-name>contextConfigLocation</param-name>  <!-- 属性标准名称 -->
    <!-- 所有的Spring配置文件只允许加载一次,不要重复加载 -->
    <param-value>classpath:spring/spring-base.xml</param-value>
  </context-param>
  <!-- 2、通过WEB容器启动的时候实现Spring容器的启动操作 -->
  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <!-- 3、配置SpringMVC的分发处理Servlet,利用此Servlet找到所有的Action -->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</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>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
原文地址:https://www.cnblogs.com/luliang888/p/11074588.html