问题描述
我正在国际化的struts2.0应用程序中工作除了中文以外,它在其他语言中也能正常工作.
I am working in struts2.0 application with internationalization Apart from Chinese it working fine in other languages.
当我将中文字符放入jsp时,我在Action中没有得到相同的值.请同样帮我.
When I put Chinese character in jsp I am not getting same values in Action. Please help me for the same.
我还使用了UTF-8 pageEncoding
I have also used the UTF-8 pageEncoding
<%@page contentType="text/html" pageEncoding="UTF-8"%>
我在jsp中有一个文本框,在其中填充了一些汉字.
I have a text box in the jsp in which I'm filling some Chinese character.
但是在服务器端的Action类中,当我尝试检索文本框的值时,我得到的是垃圾字符.
But in the Action class on the server side when I try to retrieve the value of the text box,I'm getting junk characters.
我没有10个声望,因此无法添加屏幕截图.
I'm not able to add the screen shot as I don't have 10 reputations.
任何帮助将不胜感激.
推荐答案
在操作类中,在获取参数之前,将请求主体编码设置为与JSP的pageEncoding
相同的编码.
In your action class before obtaining the parameters, set the request body encoding to the same encoding as the pageEncoding
of the JSP.
request.setCharacterEncoding("UTF-8");
希望这会有所帮助!
P.S上述解决方案仅适用于POST
请求.
P.S Above mentioned solution applies to POST
request only.
编辑:
在操作类的调用方法中获取HttpServletRequest
:
Get HttpServletRequest
in calling method of your action class:
HttpServletRequest request = ServletActionContext.getRequest();
,然后如上所述设置request
属性.
and then set request
property as mentioned above.
EDIT2 :
将此行添加到您的JSP
:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
,并将此filter
添加到您的web.xml
中:
and add this filter
in your web.xml
:
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这篇关于jsp中的中文国际化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!