当出现中文乱码问题,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. Openresty 安装第三方插件

    Openresty 安装第三方插件 程序媛没有夜生活 2016.08.02 15:33* 字数 167 阅读 1283评论 0喜欢 2 在安装之前,我们先来看一下我们现有的模块. 1.将需要安装的插件 ...

  2. 创建vue项目的时候遇到:PhantomJS not found on PATH

    1.提示找不到PhantomJS需要进行下载,如果网速允许的话可以直接 npm install -g phantomjs 如果网速不给力的话,那就先进行淘宝镜像安装 npm install -g cn ...

  3. maven设置每次构建获取最新版本号

    build.gradle中的依赖是通过设置maven依赖实现.我们知道,maven可以说是通过一个坐标定位来确定唯一一个包的,所说的坐标定位分别是groupId,artifactId和version三 ...

  4. 《Java程序设计》第一周学习记录(2)

    目录 使用JDB调试程序 系统文件被覆盖的挽救 参考资料 使用JDB调试程序 JDB是JDK自带的基于命令行的调试程序.我们先来man一下吧(说到这里,我之前在翻娄老师的博客的时候看到一篇文章:做中学 ...

  5. Oracle相关安装经验总结

    1. 安装的是oracle 12c client for windows,从同事处拿到的,说是64位的,不过我没有找到包含有64这样的文件名或者里面内容有64位的.从同事处拿到的plsqldev110 ...

  6. python SMTP

    一.一开始,相信SMTP服务,所以在本机安装了一个 apt-get install sendmail apt-get install sendmail-cf apt-get install squir ...

  7. (已解决)Xcode 运行cocos2dx弹出内部错误对话框(Internal Error)

    cocos2dx未捕获的异常升高.选择“继续”继续运行在一个不一致的状态.选择“崩溃”停止应用和崩溃报告一个错误文件. 莫名其妙,代码没有报错,运行时却弹出(内部错误)对话框出来: 再看看崩溃的底层代 ...

  8. Servlet交互与JSP

    主要内容介绍 数据共享与页面跳转 1. 为什么要有跳转: Servlet需要跳转到其它Servlet中,因为我们需要职责分明,不同Servlet来完成不同的功能 Servlet跳转到JSP中,Serv ...

  9. ProxySQL(读写分离)部署

    proxySQL是MySQL的中间件产品,是灵活强大的代理层,实现读写分离,支持Query路由功能,支持动态指定某个SQL进行缓存,支持动态加载配置,故障切换和一些SQL 过滤功能 环境: 192.1 ...

  10. Browsersync结合gulp和nodemon实现express全栈自动刷新

    Browsersync能让浏览器实时.快速响应你的文件更改(html.js.css.sass.less等)并自动刷新页面.更重要的是 Browsersync可以同时在PC.平板.手机等设备下进项调试. ...