码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC开发环境搭建

时间:2019-06-23 22:36:30      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:允许   imp   dep   hand   patch   odi   undle   component   ORC   

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">
    <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/application.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>

 

SpringMVC开发环境搭建

标签:允许   imp   dep   hand   patch   odi   undle   component   ORC   

原文地址:https://www.cnblogs.com/luliang888/p/11074588.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!