springmvc 在web配置了编码拦截器配置
来源:互联网 发布:stm8是什么样的单片机 编辑:程序博客网 时间:2024/05/29 08:39
接手一个新的项目,在项目中碰见了编码问题,在web.xml里面配置了拦截器,来统一进行编码设置,下面是具体代码:
</pre><pre name="code" class="html"> <filter> <filter-name>encodingFilter</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> <span style="color:#3333ff;"><init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param></span> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
问题总结:
代码的蓝色部分要特别注意,因为如果不写的话,会出现一种问题:
虽然springmvc 在web配置了编码拦截器,并且工程都是UTF-8格式的,但是responseText中文还是乱码,使用PrintWriter的时候也是乱码。
解决的思路有两个:
1.第一种就是我上面说的,使用这个配置。知其然更要知其所以然,具体解释:①http://blog.csdn.net/name_xiaoai/article/details/26059859 ②http://my.oschina.net/liting/blog/509865
2.第二种就是绕开这个问题,在response里获取printwriter,要注意的是要在获取printwriter之前设置response的编码。
@RequestMapping(value="/showFlow")public void showFlow(HttpServletRequest request,HttpServletResponse response){try {JSONObject result = eCharsService.showFlows(request);response.setContentType("text/html; charset=utf-8");PrintWriter print = response.getWriter();print.print(result.toString());} catch (Exception e) {// TODO: handle exceptionlog.error("异常",e);}}
亲测两种方法都可以,欢迎道友补充。
web.xml
2.controller
1 0
- springmvc 在web配置了编码拦截器配置
- SpringMVC编码拦截配置
- SpringMVC在web.xml中配置DispatcherServlet拦截了静态资源访问
- springmvc 拦截器配置
- springMVC拦截器配置
- springMvc拦截器配置
- SpringMvc拦截器配置
- SpringMvc配置拦截器
- springmvc拦截器配置
- SpringMvc拦截器配置
- springmvc拦截器配置
- springmvc拦截器配置
- springMVC拦截器配置
- springmvc配置拦截器
- SpringMVC配置拦截器
- 在springmvc配置拦截器拦截未登录用户操作
- springMVC 拦截器简单配置
- mynote springMVC拦截器配置
- 中国剩余定理概念及模板解释
- 双摄像头测距的OpenCV实现
- 探索原生Swift的模式
- Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)
- 新手入门贴:史上最全Web端即时通讯技术原理详解
- springmvc 在web配置了编码拦截器配置
- java.lang.IllegalStateException: Cannot create a session after the response has been committed
- 《Linux学习》之如何安装卸载软件及已经安装的软件、是否安装某个软件
- GB28181国标2016版本协议文档(报送稿)解读(一)
- public class与class的区别
- jQuery某网站品牌列表效果
- 云数据时代的协作办公,超级表格强力出击!
- 开发中遇到的问题
- 关于iReport-5.6.0启动界面一闪而过的解决办法