• SSM解决中文乱码


    一、tomcat配置

      配置tomcat时 设置utf-8编码,主要解决地址栏 url 中文乱码

    <!-- 配置tomcat -->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <uriEncoding>utf-8</uriEncoding>
                        <useBodyEncodingForURI>true</useBodyEncodingForURI>
                    </configuration>
                </plugin>

    二、web.xml配置

      主要解决 request response 中文乱码

    <!--中文乱码-->
        <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>

    三、springmvc.xml配置

      主要解决request response请求前经过springmvc 导致中文乱码

    xmlns:mvc="http://www.springframework.org/schema/mvc"
    
    
     xsi:schemaLocation="http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd”
    
    
    
    <!-- springmvc @responseBoyd乱码及返回自定义对象 -->
        <mvc:annotation-driven>
            <mvc:message-converters register-defaults="true">
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/plain;charset=UTF-8</value>
                            <value>text/html;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
                
                <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>
                            <value>application/json</value>
                            <value>application/xml;charset=UTF-8</value>
                        </list>
                    </property>
                    <property name="features">
                        <array>
                            <!-- 下面配置,默认是false -->
                            <value>WriteMapNullValue</value>
                            <value>WriteNullNumberAsZero</value>
                            <value>WriteNullListAsEmpty</value>
                            <value>WriteNullStringAsEmpty</value>
                            <value>WriteNullBooleanAsFalse</value>
                            <value>WriteDateUseDateFormat</value>
                        </array>
                    </property>
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>
  • 相关阅读:
    洛谷P1083 借教室
    洛谷 P1311 【选择客栈】
    一些加快 程序运行速度的方法
    【CYH-02】noip2018数论模拟赛:赛后题解
    【CYH-02】noip2018数论模拟赛:比赛通知栏
    洛谷P2888 [USACO07NOV]牛栏Cow Hurdles
    洛谷P2299 Mzc和体委的争夺战 题解
    洛谷P1113 杂务
    洛谷 P1635 跳跃
    LightOJ1119 Pimp My Ride(状压DP)
  • 原文地址:https://www.cnblogs.com/sloth-007/p/11136529.html
Copyright © 2020-2023  润新知