关于struts2中传入中文参数然后显示到页面出现乱码

来源:互联网 发布:网页设计软件哪个好 编辑:程序博客网 时间:2024/05/21 10:33

struts2传入中文出现乱码的情况

今天写了一个struts2出现了中文乱码,实现的动能是这样的
设计一个表单,其中type代表的是数字,type1输入的是中文,其中表单中action是一个struts2的action,单击提交以后返回成功视图的后吧type和type1中的数据取出来

其中action的部分代码:
<action name="useraction" class="action.UserAction"> 
<result name="success" type="dispatcher">/success.jsp</result><!-- 这个是服务器跳转,传递参数是在valuestack中 -->
<result name="test" type="redirect">
<param name="location">success.jsp</param>
<param name="type">${type}</param>
<param name="type1">${type1}</param>
</result>
<result name="error">/error.jsp </result>
</action>
这里用到的result type是dispatcher这个是默认的类型,是服务器跳转,其中的参数都保存到valuestack中(值栈)

在success页面中取出type和type1的值
success.jsp的部分代码
 取出值 <s:property value="type"/><br/>
  汉字<s:property value="type1"/><br/>

这样什么都不做取出后显示中文乱码,struts2其实也做了配置编码的设计(<constant name="struts.i18n.encoding" value="utf-8"/>配置常量)但是但是在struts2
中出现bug,这样配置以后任然会出现乱码,我查的是在web.xml中配置的struts2过滤器出现问题,
<filter-class>中配置以前的版本就能够解决中文乱码,但是我返回struts2.0的配置以后仍然会出现中文乱码

最后我想到用过滤器的方法进行编码的过滤,在web.xml中配置一个过滤器
部分代码如下
web.xml
<!-- 编码过滤器 -->
 <filter>
<filter-name>encoding</filter-name>
<filter-class>filterdemo.EcodingFilter</filter-class>
<init-param>
<param-name>charset</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

EcodingFilter.java文件


这里注意的一点是进行编码设计以后必须把请求继续传递下去,chain。doFilter()函数进行传递
这样设置以后就不会出现中文乱码了

0 0