解决问题:SpringMvc中转发的html文件中文是乱码

2019-09-17 10:12:06来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

解决问题:SpringMvc中转发的html文件中文是乱码

目录

  • 1.环境说明,以及前言
  • 2.问题描述:
  • 3.失败的方法(这里写失败并不代表在其他情况不管用)
    • 3.1 html网页本身编码不是UTF-8(推荐尝试)
    • 3.2 web.xml中没有设置配置编码方式(ssm框架配置必定配置)
    • 3.3修改html 文件(感觉没屌用)
    • 3.4 修改html文件中的<head>(html文件一定要加这个)
    • 3.5 发送内容设置为中文
    • 3.6 JSP翻译为Servlet时的编码(jsp下的配置)
    • 3.7 jsp下后台设置编码(jsp下serverlet是肯定要写的,可以用配置字符拦截器之类的替代这个)
    • 3.8 给tomcat的server.xml文件设置编码格式(推荐尝试下)
  • 4 问题就是这样解决的

@(SpringMvc中设置了html视图解析器却不起作用(看了几十多篇文章的相关内容的精华合集))

1.环境说明,以及前言

使用ssm框架、IDEA、JKD1.8、因为这个模块写的是上传模板、先开始用ajax传,但是不知为啥不能注入,只能选择表单,然后跳转,所以要两个页面,成功页面和失败页面。

2.问题描述:

成功之后跳转页面html文件的中文呈现乱码,但是我直接访问静态资源是中文不是乱码,我就查了近乎几十篇文章,找到了许多方法。

3.失败的方法(这里写失败并不代表在其他情况不管用)

3.1 html网页本身编码不是UTF-8(推荐尝试)

描述:这个办法就是html的网页格式本来就不是UTF-8,自然访问的时候,显示是乱码
解决办法:用记事本打开,然后点击另存为,设置为UTF-8

3.2 web.xml中没有设置配置编码方式(ssm框架配置必定配置)

 <!-- 配置编码方式-->
  <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>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>*.html</url-pattern>
  </filter-mapping>

3.3修改html 文件(感觉没屌用)

在百度的时候也查到了这这一篇zh-CN
说将en改为
至于原因可以看下面这一篇文章(但是这个根本没有帮助,这个解释是说这是给搜索引擎有帮助)
https://www.w3school.com.cn/tags/html_ref_language_codes.asp

3.4 修改html文件中的<head>(html文件一定要加这个)

在head标签中加入:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

至于这个代码代表什么意思具体可以查看项目这个文章
https://www.cnblogs.com/liubeimeng/p/3894177.html
我总结大概意思就是说,这个meta是html的元标签,其中包含了对应html的相关信息,客户端浏览器或服务端程序都会根据这些信息进行处理.==charset(编码):这个网页的编码是UTF-8,需要注意的是这个是网页内容的编码,而不是文件本身的.==就是说设定这个文件内容的编码为UTF-8即汉字编码

3.5 发送内容设置为中文

比如你用form表单发送内容,你加上accept-charset="UTF-8"这句代码代表
accept-charset 属性规定服务器处理表单数据所接受的字符集。

    <form action="" method="post" enctype="multipart/form-data" accept-charset="UTF-8">

enctype这个属性表示form表单的数据以什么形式发送,我这边有文件上传所以要加这句,如果你们没有文件上传就要把这个删除

3.6 JSP翻译为Servlet时的编码(jsp下的配置)

使用jsp就要在头文件上加入这句代码,相当于JSP翻译为Servlet时的编码
==jsp和html网页最大的区别就是一个是动态生成的,一个是静态网页==
<%@ page language="java" pageEncoding="utf-8"%>

3.7 jsp下后台设置编码(jsp下serverlet是肯定要写的,可以用配置字符拦截器之类的替代这个)

后台可以手动设置request字符集

request.setCharacterEncoding("UTF-8");

3.8 给tomcat的server.xml文件设置编码格式(推荐尝试下)

我找的文章说的是get方式
GET提交方式中文乱码解决
通过修改request.setCharacterEncoding(“UTF-8”);只能解决POST提交方式的请求编码,对GET无效。

1 、最简单解决方案: 该解决方案不再需要任何额外配置(如filter)
TOMCAT默认ISO-8859-1 因此可以设置默认编码为UTF-8解决,在conf\server.xml文件中设置如下

Java代码

<Connector port="8080" protocol="HTTP/1.1"  
                               connectionTimeout="20000"  
                               redirectPort="8443" URIEncoding=”UTF-8”/>  

2 、在 tomcat4 之后,可以通过如下配置来解决中文乱码,但必须配置 filter
在conf\server.xml文件中设置如下

Java代码

<Connector port="8080" protocol="HTTP/1.1"  
                                       connectionTimeout="20000"  
                                       redirectPort="8443" useBodyEncodingForURI=“true”  />  

然后request.setCharacterEncoding(“UTF-8”)也可解决GET乱码
参考资料:https://www.iteye.com/topic/1121242#2322208

4 问题就是这样解决的

首先是视图解析器(我看网上的视图解析器很多配置都是jsp的,过几天我写一个关于配置视图解析器的)
因为是ssm框架,我没有使用重定向来跳转页面,所以要配置视图解析器
==我在网上找到的视图解析器坑了我一手,他只有:==

   <bean id="freemarkerConfig"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath">
      <value>/WEB-INF/view</value>
    </property>
  </bean>
  <bean id="htmlViewResolver"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="contentType" value="text/html;charset=UTF-8"></property>
    <property name="suffix" value=".html"></property>
    <property name="order" value="0"></property>
  </bean>

==没有动态化本地设置==

    <property name="freemarkerSettings">
      <props>
        <prop key="locale">zh_CN</prop>
        <prop key="defaultEncoding">UTF-8</prop>
      </props>
    </property>

我尝试过上述全部方法后,最后在这里成功了
分享我的视图解析器:

     <bean id="freemarkerConfig"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath">
      <value>/WEB-INF/view</value>
    </property>
    
    <property name="freemarkerSettings">
      <props>
        <prop key="locale">zh_CN</prop>
        <prop key="defaultEncoding">UTF-8</prop>
      </props>
    </property>
    
  </bean>
  <bean id="htmlViewResolver"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="contentType" value="text/html;charset=UTF-8"></property>
    <property name="suffix" value=".html"></property>
    <property name="order" value="0"></property>

  </bean>

==同时需要导入一个jar包,maven如下:==

    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.20</version>
    </dependency>

还要在web.xml中添加如下代码:(如果没有的话

 <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>*.html</url-pattern>
  </filter-mapping>
  1. 什么是Freemarker
    FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出。FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java 等。
    目前企业中:主要用Freemarker做静态页面或是页面展示

    5.后记

    写这篇文章希望能帮到大家,我今天坐在电脑前面一天,就为了搞定这个bug,希望能让大家少走点弯路。有什么问题都可以在评论区和我交流

分享我的pom.xml配置以及ssm配置和web.xml配置:
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied.  See the License for the
  specific language governing permissions and limitations
  under the License.
-->
<!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <packaging>war</packaging>

  <name>ITKIMROOT</name>
  <groupId>com.itkim.rootuser</groupId>
  <artifactId>ITKIMROOT</artifactId>
  <version>1.0-SNAPSHOT</version>



  <properties>
    <!-- spring版本号 -->
    <spring.version>4.2.5.RELEASE</spring.version>
    <!-- mybatis版本号 -->
    <mybatis.version>3.2.4</mybatis.version>
    <!-- log4j日志文件管理包版本 -->
    <slf4j.version>1.6.6</slf4j.version>
    <log4j.version>1.2.12</log4j.version>
  </properties>

  <dependencies>
    <!-- spring核心包 -->
    <!-- springframe start -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-oxm</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!-- springframe end -->

    <!-- mybatis核心包 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>
    <!-- mybatis/spring包 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.2.2</version>
    </dependency>
    <!-- mysql驱动包 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.12</version>
    </dependency>
    <!-- junit测试包 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!-- jstl -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.2.1-b03</version>
      <scope>provided</scope>
    </dependency>

    <!-- servlet -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>
    <!-- json数据 -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.7.1</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.7.1</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.7.1</version>
    </dependency>
    <dependency>
      <groupId>net.sf.json-lib</groupId>
      <artifactId>json-lib</artifactId>
      <version>2.4</version>
      <classifier>jdk15</classifier>
    </dependency>
    <!-- commons -->
    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.4</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1</version>
    </dependency>
    <dependency>
      <groupId>commons-pool</groupId>
      <artifactId>commons-pool</artifactId>
      <version>1.5.6</version>
    </dependency>
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>
    <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils</artifactId>
      <version>1.8.3</version>
    </dependency>
    <dependency>
      <groupId>commons-httpclient</groupId>
      <artifactId>commons-httpclient</artifactId>
      <version>3.1</version>
    </dependency>
    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.1</version>
    </dependency>
    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.9</version>
    </dependency>
    <dependency>
      <groupId>javax.annotation</groupId>
      <artifactId>jsr250-api</artifactId>
      <version>1.0</version>
    </dependency>

    <dependency>
      <groupId>net.sf.ezmorph</groupId>
      <artifactId>ezmorph</artifactId>
      <version>1.0.6</version>
    </dependency>
    <dependency>
      <groupId>javax.activation</groupId>
      <artifactId>activation</artifactId>
      <version>1.1</version>
    </dependency>
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>
    <!-- 日志文件管理包 -->
    <!-- log start -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <!--上传-->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>
    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.20</version>
    </dependency>
    <!-- log end -->
  </dependencies>
  <build>

    <finalName>helloworld</finalName>
    <plugins>
      <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.2</version>
        <configuration>
          <!--允许移动生成的文件-->
          <verbose>true</verbose>
          <!--允许覆盖生成的文件-->
          <overwrite>true</overwrite>
        </configuration>
      </plugin>
    </plugins>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>
  </build>
</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>helloworld</display-name>
  <!-- 配置编码方式-->
  <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>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>*.html</url-pattern>
  </filter-mapping>



  <!-- 配置springmvc的前端控制器 指向spring-mvc.xml 程序在启动的时候就加载springmvc 可以接受所有请求 load-on-startup:表示启动容器时初始化该Servlet; -->
  <servlet>
    <servlet-name>springServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 可以自定义servlet.xml配置文件的位置和名称, 默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value> classpath:/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 将前端URL请求和后台处理方法controller建立对应关系-->
  <servlet-mapping>
    <servlet-name>springServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!-- Spring配置 -->
  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <!-- 取消对某一类文件的拦截-->
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.md</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value> classpath:/applicationContext.xml</param-value>
  </context-param>
  <!-- 欢迎页面-->
  <welcome-file-list>
    <welcome-file>/index.html</welcome-file>
  </welcome-file-list>
  <!--404错误展示页面,可自行配置-->
  <!--<error-page>-->
  <!--<error-code>404</error-code>-->
  <!--<location>/WEB-INF/views/404.jsp</location>-->
  <!--</error-page>-->
  <!--设置session失效时间为30分钟 -->
  <session-config>
    <session-timeout>600</session-timeout>
  </session-config>
</web-app>
       

springmvc的设置 applicationContext.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:beans="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-4.3.xsd
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"  >


  <!-- ①:对com.springmvc包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
  <context:component-scan base-package="com.darksouls"/>

  <mvc:annotation-driven />
  <!-- 静态资源访问 -->
  <!--如果webapp下你新建了文件夹,想访问里面的静态资源,那么就要在这配置一下-->
  <mvc:resources location="/images/" mapping="/images/**"/>
  <mvc:resources location="/css/" mapping="/css/**"/>
  <mvc:resources location="/js/" mapping="/js/**"/>
  <mvc:resources location="/WEB-INF/view/" mapping="/**.html" />






  <!-- Configures the @Controller programming model
  <mvc:annotation-driven />-->
  <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
      <list>
        <ref bean="mappingJacksonHttpMessageConverter"/>
      </list>
    </property>
  </bean>

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





  <!-- 配置视图解析器,把控制器的逻辑视频映射为真正的视图 -->
  <!-- /WEB-INF/jsp/start.jsp -->
  <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
    <!--<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>-->
    <!--<property name="prefix" value="/WEB-INF/views/" />-->
    <!--<property name="suffix" value=".jsp" />-->
  <!--</bean>-->
  <bean id="freemarkerConfig"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath">
      <value>/WEB-INF/view</value>
    </property>

    <property name="freemarkerSettings">
      <props>
        <prop key="locale">zh_CN</prop>
        <prop key="defaultEncoding">UTF-8</prop>
      </props>
    </property>

  </bean>
  <bean id="htmlViewResolver"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="contentType" value="text/html;charset=UTF-8"></property>
    <property name="suffix" value=".html"></property>
    <property name="order" value="0"></property>

  </bean>





  <!-- 配置dbcp数据库连接池 -->

  <!-- <context:property-placeholder location="classpath:db.properties"/> -->
  <!--数据库配置 -->
  <bean id = "propertyConfigurer" class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
      <list>
        <value>classpath:jdbc.properties</value>
      </list>
    </property>

  </bean>

  <!-- 数据库连接池 -->
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${driver}"/>
    <property name="url" value="${url}"/>
    <property name="username" value="${username}"/>
    <property name="password" value="${password}"/>
    <property name="initialSize" value="1"/>
    <property name="maxActive" value="100"/>
    <property name="maxIdle" value="5"/>
    <property name="maxWait" value="80000"/>
  </bean>

  <!-- 配置事务管理器 -->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>

  <!-- 拦截器 -->
  <mvc:interceptors>
    <!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
  </mvc:interceptors>
  <!-- 定义无Controller的path<->view直接映射 -->
  <!-- <mvc:view-controller path="/" view-name="redirect:/" /> -->

  <!-- 配置会话工厂SqlSessionFactory -->

  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 数据源 -->
    <property name="dataSource" ref="dataSource"/>
    <property name="mapperLocations" value="classpath:sqlmap/*.xml"/>
    <property name="typeAliasesPackage" value="com.darksouls.entity" />
  </bean>

  <!-- 在spring容器中配置mapper的扫描器产生的动态代理对象在spring的容器中自动注册,bean的id就是mapper类名(首字母小写)-->
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 指定扫描包的路径,就是mapper接口的路径,多个包中间以 半角逗号隔开   -->
    <property name="basePackage" value="com.darksouls.dao"/>
    <!-- 配置sqlSessionFactoryBeanName -->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  </bean>
  <!--文件解析器-->
  <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize">
      <value>5242880</value>
    </property>
  </bean>

</beans>

原文链接:https://www.cnblogs.com/lzy321/p/11487770.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Struts2 拦截器

下一篇:Java多线程编程(3)--线程安全性