Chinese internationalization issue in jsp

柔情痞子 提交于 2019-12-11 18:22:48

问题


I am working in struts2.0 application with internationalization Apart from Chinese it working fine in other languages.

When I put Chinese character in jsp I am not getting same values in Action. Please help me for the same.

I have also used the UTF-8 pageEncoding

<%@page contentType="text/html" pageEncoding="UTF-8"%>

I have a text box in the jsp in which I'm filling some Chinese character.

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.

I'm not able to add the screen shot as I don't have 10 reputations.

Any help will be appreciated.


回答1:


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");

Hope this helps!

P.S Above mentioned solution applies to POST request only.

EDIT:

Get HttpServletRequest in calling method of your action class:

HttpServletRequest request = ServletActionContext.getRequest();

and then set request property as mentioned above.

EDIT2:

Add this line to your JSP:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>

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>



回答2:


Try the following thing in your jsp page

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">



回答3:


Recently i faced an issue i.e. from jsp when I copy pasting a chinese character in name textbox and when i am trying to fetch that in Java DAO layer i get some junk value for name. With below code i have fixed the issue and i can get the chinese value same as in jsp.

String name = new String(entityObj.getName().getBytes("iso-8859-1"), "UTF-8");


来源:https://stackoverflow.com/questions/10732557/chinese-internationalization-issue-in-jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!