前端处理:
JSP : <%@page pageEncoding="utf-8" %>
HTML : <meta charset="UTF-8">
后端处理:
GET请求(request)乱码处理:
<!-- Tomcat的sever.xml中添加配置:URIEncoding=utf-8 -->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8"/>
POST请求(request)乱码处理:
<!-- 在web.xml中设置,添加以下配置,此过滤器会进行:request.setCharactorEncoding("utf-8"); -->
<filter>
<filter-name>encoding</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>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
响应(response)乱码处理:
<!-- 在springmvc配置文件中添加如下配置 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
文章来源地址https://www.toymoban.com/news/detail-691075.html
文章来源:https://www.toymoban.com/news/detail-691075.html
到了这里,关于SpringMVC中文乱码(request或response)前后端处理的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!