• springMVC @response 中文乱码解决


    以下任选一种

    在web.xml中:

    添加一个过滤器(filter),注册 org.springframework.web.filter.CharacterEncodingFilter

     <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <async-supported>true</async-supported>
        <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>

    全局修改输出为UTF-8编码:

    <?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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
        <!-- utf-8编码 -->
        <mvc:annotation-driven>
            <mvc:message-converters register-defaults="true">
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <constructor-arg value="UTF-8" />
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>
    </beans>

    编码问题

    如何设置,才能避免乱码?

    Producer/Consumer (生产/消费模式)。

    // 请求内容类型必须为 text/html
    @RequestMapping(value="/aaa", consumes="text/html");
    
    // 客户端接收 json 且编码为 utf-8
    @RequestMapping(value="/bbb", produces="application/json; charset=UTF-8");
  • 相关阅读:
    flex 鼠标事件和效果
    查看和修改Oracle服务器端的字符集
    Oracle随机抽取数据sql
    hibernate.properties
    Oracle11g物理文件冷备份(转)
    支付宝接口使用文档说明 支付宝异步通知(notify_url)与return_url
    关于Oracle的行转列
    Intellij idea 12 tomcat日志窗口不显示问题
    在Servlet中获取来源URL
    uniapp 实现小程序列表项左滑菜单
  • 原文地址:https://www.cnblogs.com/nongzihong/p/10069018.html
Copyright © 2020-2023  润新知