当出现中文乱码问题,Spring中可以利用CharacterEncodingFilter过滤器解决,如下代码所示:

  <!-- Spring字符编码过滤器:解决中文乱码问题 -->
<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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

但是,该过滤器唯一无法解决一种特定请求,即在地址栏中以GET方式传中文的请求,例如:

  localhost:8080/MyApp/user/toAdd.action?name=小明

CharacterEncodingFilter源码如下:

/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed 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.
*/ package org.springframework.web.filter; import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet 2.3/2.4 Filter that allows one to specify a character encoding for
* requests. This is useful because current browsers typically do not set a
* character encoding even if specified in the HTML page or form.
*
* <p>This filter can either apply its encoding if the request does not
* already specify an encoding, or enforce this filter's encoding in any case
* ("forceEncoding"="true"). In the latter case, the encoding will also be
* applied as default response encoding on Servlet 2.4+ containers (although
* this will usually be overridden by a full content type set in the view).
*
* @author Juergen Hoeller
* @since 15.03.2004
* @see #setEncoding
* @see #setForceEncoding
* @see javax.servlet.http.HttpServletRequest#setCharacterEncoding
* @see javax.servlet.http.HttpServletResponse#setCharacterEncoding
*/
public class CharacterEncodingFilter extends OncePerRequestFilter { private String encoding; private boolean forceEncoding = false; /**
* Set the encoding to use for requests. This encoding will be passed into a
* {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call.
* <p>Whether this encoding will override existing request encodings
* (and whether it will be applied as default response encoding as well)
* depends on the {@link #setForceEncoding "forceEncoding"} flag.
*/
public void setEncoding(String encoding) {
this.encoding = encoding;
} /**
* Set whether the configured {@link #setEncoding encoding} of this filter
* is supposed to override existing request and response encodings.
* <p>Default is "false", i.e. do not modify the encoding if
* {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()}
* returns a non-null value. Switch this to "true" to enforce the specified
* encoding in any case, applying it as default response encoding as well.
* <p>Note that the response encoding will only be set on Servlet 2.4+
* containers, since Servlet 2.3 did not provide a facility for setting
* a default response encoding.
*/
public void setForceEncoding(boolean forceEncoding) {
this.forceEncoding = forceEncoding;
} @Override
protected void doFilterInternal(
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException { if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
request.setCharacterEncoding(this.encoding);
if (this.forceEncoding) {
response.setCharacterEncoding(this.encoding);
}
}
filterChain.doFilter(request, response);
} }

关于web.xml中配置Spring字符编码过滤器以解决中文乱码的问题的更多相关文章

  1. web.xml中配置Spring中applicationContext.xml的方式

    2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...

  2. Spring编码过滤器:解决中文乱码

    Spring编码过滤器:解决中文乱码 针对问题: 前台JSP页面和JAVA代码中使用了不同的字符集进行编码的时候就会出现表单提交的数据或者上传/下载中文名称文件出现乱码的问题: 解决方案: web.x ...

  3. Java web应用中的常见字符编码问题的解决方法

    以下是 Java Web应用的常见编码问题 1. html页面的编码 在web应用中,通常浏览器会根据http header: Content-type的值来决定用什么encoding, 比如遇到Co ...

  4. [转载]Java web应用中的常见字符编码问题的解决方法

    以下是 Java web应用的常见编码问题 1. html页面的编码 在web应用中,通常浏览器会根据http header: Content-type的值来决定用什么encoding, 比如遇到Co ...

  5. web.xml中配置spring.xml的三种方式

    我们知道spring在web.xml中可以有三种方式来配置其xml路径:org.springframework.web.servlet.DispatcherServletorg.springframe ...

  6. 0077 web.xml中配置Spring MVC时,Servlet-name上报Servlet should have a mapping的错误

    这次是手工建立的web工程目录,在配置webapp/WEB-INF/web.xml的Spring MVC的DispatcherServlet时,在servlet-name上报错:Servlet sho ...

  7. web.xml中配置spring配置(application.xml)文件

    application.xml 一般放到WEB-INF下,当然,你也可以将它放到任意问题,但需要web.xml指向到该文件 1.application.xml配置 <?xml version=& ...

  8. Linux下修改MySQL数据库字符编码为UTF-8解决中文乱码

    由于MySQL编码原因会导致数据库出现乱码. 解决办法: 修改MySQL数据库字符编码为UTF-8,UTF-8包含全世界所有国家需要用到的字符,是国际编码. 具体操作: 1.进入MySQL控制台 &g ...

  9. 在web.xml中配置spring配置文件的路径

    <context-param>     <param-name>contextConfigLocation</param-name>     <param-v ...

随机推荐

  1. function module 之间调用

    1: 在一个function group 中定义一个function module 2:在另外一个module中调用该module "调用其它function 要用 单引号 引着. 一个mo ...

  2. Python 命名笔记

    类名开头大写,驼峰命名 函数名,变量名都小写, 全局常量 如COUNT, 使用全部大写 https://www.cnblogs.com/lytwajue/p/7324724.html 这个是全局变量关 ...

  3. UML序列图的理解:

    UML序列图的理解:UML序列图是指一个对象的方法在处理过程中调用其他对象的图:重点是要突出调用其他对象的序列:

  4. [LeetCode] 840. Magic Squares In Grid_Easy

    A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, co ...

  5. js 图片区域可点击,适配移动端,图片大小随意改变

    实现图片区域可点击,实际上使用map是可以的,但是适配效果并不好,图片只能是固定大小的值,而且点都被写死了. 在这里,我使用的js基于canvas写的一个小工具.可以圈出你需要点击的部分,然后生成一串 ...

  6. gcc dynamic load library

    Linux下一般都是直接在编译生成时挂接上链接库,运行时,把链接库放到系统环境里就可以了 但是windows出现带来了动态链接的概念,也就兴起了非windows世界的插件的概念的范潮 对应于windo ...

  7. 用URLRewriter重写url

    用url重新一般都是使用URLRewriter库,基本上都是一些配置,在webconfig中 首先配置configuration节点 <configSections> <sectio ...

  8. (2)Python3笔记 数据类型之"组"(序列) 与 集合

    一. list [ 列表 ] ---- 任意数据类型的有序排列 1. list 操作类似 str操作 (参考上一节字符串操作) [1,2,3,4] [1,2,'a', True, [2,'b']]   ...

  9. 发布网站配置文件和SSL

    1.将cert下新建一个文件将所有证书文件放在新建的文件下 例如:cert/medcard 2.配置网站的.conf文件 <VirtualHost *:443> ServerName ww ...

  10. c# Mongodb批量更新

    public void Put(List<OnlineItem> datas)         {             try             {                ...