zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    springMVC必须的jar包
    project---clean
    maven项目 启动报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
    @ResponseBody 注解
    HttpServletRequest 需要导入xx包?
    Maven中GroupID和ArtifactID
    Java中包、类、方法、属性、常量的命名规则
    使用 @RequestMapping 注解,需要导入的包:spring-webmvc
    【杂题】【动态规划】【搜索】——洛谷P1441砝码称重
    搞清clientHeight、offsetHeight、scrollHeight、offsetTop、scrollTop
  • 原文地址:https://www.cnblogs.com/sloth-007/p/11136529.html
Copyright © 2011-2022 走看看