在使用struts2的 struts-tags的是碰到了乱码问题。
心里愤愤,觉得这个乱码问题都解决过几次了,怎么还会被撞上了?!
先把以往需要设置的点罗列一下:
1. JSP 文件头
2. struts.properties中的编码设定
struts.locale = UTF-8
struts.i18n.encoding=utf-8
3. Resource的native2ascii 转码
通常这些搞定就能解决编码问题了。
再过一点在web.xml里面配置一个编码的filter:
struts-cleanup
org.apache.struts2.dispatcher.ActionContextCleanUp
struts-cleanup
/*
不过这次我碰到了一个这些都不能解决的问题。仍然是乱码。
仔细回想了一下自己的各个环节有什么不同,
我在eclipse里输入中文的,所以把中文素材文件设置成了utf-8格式。
这个在editplus中看起来是乱码的,刚好乱码的结果和web显示的一样。
于是问题找到了,使用editplus重新改写了一下中文。
然后native2ascii一下放上去果然就好了。
本文来自: IT知道网(http://www.itwis.com) 详细出处参考:http://www.itwis.com/html/java/struts/20080313/1081.html
另一种方法
jsp向action提交数据时出现乱码的解决办法:
在web.xml中,添加filter
<!-- encodingFilter -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<!-- 下面2个filter-mapping顺序不能颠倒 -->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
最后,还有一种方法
jsp文件中使用struts2 <s:form>表单,设置
<%@ page language="java" pageEncoding="GBK" contentType="text/html;charset=GBK"%>
向mysql数据库中插入数据时,中文总是乱码,开始以为是数据库的问题,因为mysql经常出现这样的问题,头都大了,后来重新配置数据库,编码都设为gbk,重新建库,结果还是乱码!气的想把电脑给砸了!
试着从数据库的控制台输入中文,居然能正确显示,总算有点希望了,说明数据库没问题了,那应该是程序有问题!
在action中接收表单值的setter方法处设置断点,用debug一查variable,结果这里是乱码!百思不得其解,jsp头都设置成gbk了,为什么提交的中文是乱码呢?查struts2的doc,找到form的属性,没有发现有设置编码的地方啊!去网上搜索,“struts 2 form 中文乱码”,原来是default.properties文件中设置了struts.i18n.encoding=UTF-8,于是在struts.xml中设置<constant name="struts.i18n.encoding" value="GBK"/>,再次调试,用debug一查,果然是中文了,呵呵!
总结:
问题:jsp中设置编码为gbk,用struts2的form表单提交中文出现乱码
原因:struts2默认提交的字符编码是utf-8,与gbk不一致!
解决办法:在struts.xml或是struts.properties文件中讲struts.i18n.encoding设置为gbk,一切就ok了!
分享到:
相关推荐
struts中文乱码问题解决详细步骤(两种方法)
关于StrutsPrepareAndExecuteFilter最新的过滤器,在Struts.xml定义常量<constant name="struts.i18n.encoding" value="UTF-8"/>...关于Struts2各个版本、各种过滤器,用get方式提交都方法都无法解决的中文乱码问题。
解决SSH中的struts2的中文乱码问题,里面暂且介绍了3种,其他的希望有人能提出来,让大家共享...
struts处理中文乱码问题总结struts处理中文乱码问题总结struts处理中文乱码问题总结struts处理中文乱码问题总结
struts的中文乱码问题。。我想这对大多数朋友会有用的
struts下的汉字乱码问题 经过测试
struts 中文乱码问题解决
在进行web应用程序开发的过程中,中文问题经常困扰着很多程序员。因此,在接下来的两个课程的学习中,我将根据实践经验来谈一下web应用中的中文问题的解决方法。让大家不再为此而头痛。
eclipse struts 中文乱码问题图解 页面乱码 参数乱码 国际化乱码
struts2中存在中文乱码的问题解决方 中文乱码,首先要区分是页面乱码、action乱码,还是数据库乱码。 大致的原理是java使用unicode编码-->window使用gbk(gb2312的扩展集)--mysql默认使用utf-8(unicode的一种编码...
struts2中文乱码的解决DEMO,绝对可以运行。
struts2中文乱码
奇怪的struts2的中文乱码问题及解决.docx
Struts中中文乱码的解决方法 Struts中中文乱码的解决方法 Struts中中文乱码的解决方法
解决中文乱码的问题
struts1.2 解决中文乱码(Java技术)
解决Struts2.0中文乱码详细方法。
Struts中文乱码解决方案