spring MVC 乱码问题
(转)
spring的字符集过滤通过用于处理项目中的乱码问题,该过滤器位于org.springframework.web.filter包中,指向类CharacterEncodingFilter,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);
- }
- }
上述代码显示,在配置字符集过滤器时可设定两个参数的值,如下:
l encoding:字符集,即将过滤到的request的字符集设置为encoding指定的值,如UTF-8等,相当于:
- request.setCharacterEncoding
l forceEncoding:字面意思是强制字符集,但你大可不必按字面意思理解,因为这个参数的值只不过是指定response的字符集是否也设置成encoding所指定的字符集,所以你可以选择设置为true或false,当值为true时,相当于
- request.setCharacterEncoding(“”);
- response.setCharacterEncoding(“”);
当值为false时,相当于:
- request.setCharacterEncoding(“”);
默认值为false。
示例:
- <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>
以上代码放置在web.xml中,相当于servlet中的:
- request.setCharacterEncoding("UTF-8");
- response.setCharacterEncoding("UTF-8");
spring MVC 乱码问题的更多相关文章
- Spring MVC 用post方式提交表单到Controller乱码问题,而get方式提交没有乱码问题
在web.xml中添加一个filter,即可解决post提交到Spring MVC乱码问题 <!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码--> <filter> ...
- Spring MVC 解决乱码
1. Spring 事务处理 Spring MVC乱码问题 三种处理数据库的方式 (1)jdbc(J2EE规范) (2)Spring JDBCTemplate( ...
- Spring MVC 学习总结(三)——请求处理方法Action详解
Spring MVC中每个控制器中可以定义多个请求处理方法,我们把这种请求处理方法简称为Action,每个请求处理方法可以有多个不同的参数,以及一个多种类型的返回结果. 一.Action参数类型 如果 ...
- MVC已经是现代Web开发中的一个很重要的部分,下面介绍一下Spring MVC的一些使用心得。
MVC已经是现代Web开发中的一个很重要的部分,下面介绍一下Spring MVC的一些使用心得. 之前的项目比较简单,多是用JSP .Servlet + JDBC 直接搞定,在项目中尝试用 Strut ...
- 框架-Java:Spring MVC
ylbtech-框架-Java:Spring MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 We ...
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
spring mvc使用的默认处理字符串编码为ISO-8859-1 解决方法: 第一种方法: 对于需要返回字符串的方法添加注解,如下: @RequestMapping(value="/use ...
- spring mvc form表单提交乱码
spring mvc form表单submit直接提交出现乱码.导致乱码一般是服务器端和页面之间编码不一致造成的.根据这一思路可以依次可以有以下方案. 1.jsp页面设置编码 <%@ page ...
- Spring MVC Controller中解析GET方式的中文参数会乱码的问题(tomcat如何解码)
Spring MVC Controller中解析GET方式的中文参数会乱码的问题 问题描述 在工作上使用突然出现从get获取中文参数乱码(新装机器,tomcat重新下载和配置),查了半天终于找到解决办 ...
- spring mvc 中文乱码 post与get的方法解决
spring mvc表单提交中文参数乱码问题 今天测试spring mvc ,中文乱码,在web.xml中加上 <filter> <filter-name>encodingF ...
随机推荐
- 现代3D图形编程学习--opengl使用不同的缓存对象(译者添加)
现代3D图形编程学习系列翻译地址 http://www.cnblogs.com/grass-and-moon/category/920962.html opengl使用不同的缓存对象 在设置颜色一章中 ...
- Webdriver API之元素定位
Webdriver提供了8种元素定位方法:id.name.class name.tag name.link text.partial link text.xpath.css selector 一.以上 ...
- nginx学习笔记——http module分析
源码:nginx 1.12.0 nginx由于其高性能.扩充性好等特点在迅速走红,越来越多的公司采用nginx作web服务器.负载均衡.waf等 工作,一些基于nginx ...
- bigdecimal更精确的浮点处理方式
Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算.双精度浮点型变量double可以处理16位内有效数,超过16位,double可能会出现内存 ...
- 【web】之 jquery上传插件的Plupload的使用
首先下载plupload->http://www.plupload.com 因为Plupload可配置参数比较多,所以这里讲解最常用的,结合jquery-ui展示的界面!如下: Plupload ...
- php流程管理
流程控制即某个人发起一个流程,通过一层一层审核,通过后,完成整个流程,若有一层审核未通过,中断整个流程.即结束! 比如请假流程: 某一员工发起一个请假流程,那么这个流程的节点人员即他的上级,上上级,上 ...
- xmlplus 组件设计系列之五 - 选项卡
这一章将设计一个选项卡组件,选项卡组件在手持设备上用的比较多,下面是一个示意图: 选项卡组件的分解 在具体实现之前,想像一下目标组件是如何使用的,对于设计会有莫大的帮助.通过观察,可以将选项卡组件分为 ...
- Python全栈之路-Day31
1 反射 反射的精髓是通过字符串去获取对象属性的值 1.1 基于类和对象反射的属性 #!/usr/bin/env python # __Author__: "wanyongzhen" ...
- [Mobile Web]Web中如何分辨移动设备?(iPad、iPhone、Android)
第一种, Javascript [javascript] view plaincopyprint?在CODE上查看代码片派生到我的代码片 var deviceAgent = navigator.use ...
- PTA自测-3 数组元素循环右移问题
自测-3 数组元素循环右移问题 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M≥0)个位置,即将A中的数据由(A0A1···AN-1)变换为 ...