当前位置: 代码迷 >> Web前端 >> 采取struts1.2+prototype,中文信息为双数时,显示正常,单数时,变成花符
  详细解决方案

采取struts1.2+prototype,中文信息为双数时,显示正常,单数时,变成花符

热度:285   发布时间:2012-11-23 22:54:33.0
采用struts1.2+prototype,中文信息为双数时,显示正常,单数时,变成花符
采用struts1.2+prototype,页面采用gbk编码,并且tomcat采用了gbk过滤。
发现当中文信息为双数时,显示能正常显示,当为单数时,最后一个汉字就花了。
程序:

private void runNameLookupState(ControllerRequest request, ControllerResponse response)
throws ControllerException, NonHandleableException {

String theId = request.getParameter("theId");
String s1 = URLUTF8Encoder.decode(theId);
String ss = getGbkToUtf8(theId);

ServletControllerRequest sr = (ServletControllerRequest) request;
HttpServletResponse hres = (HttpServletResponse) sr.getServletResponse();
hres.setContentType("text/html;charset=GBK");

try{
PrintWriter out = hres.getWriter();
JSONObject json = new com.jsite.json.JSONObject();
String sss = getUtf8ToGbk(ss);
json.put("fullName", "Input : "+ss);

//hres.setHeader("X-JSON", json.toString());

out.print(json.toString());
out.flush();
out.close();
}catch(Exception e){
e.printStackTrace();
}

...

页面:

<script src="<%=contextPath%>/jsite/js/prototype-1.5.1-formatted.js"></script>
<script>
function lookup()
{
var pas = "theId=" +encodeURI( $('theId').value);

new Ajax.Request('<html:rewrite page="/util/namelookup.do?state=NameLookup"/>', {
   parameters: pas,
   method:'post',
   onComplete: function(dataResponse){
   var data = eval('(' + dataResponse.responseText + ')');
   alert(data.fullName);
   $('fullName').value = data.fullName;
   }
});
}
</script>

<input type="text" id="theId" />
<input type="text" id="fullName" name="fullName"/>
<input type="button" value=Test2 onclick="lookup();" />
1 楼 freecode 2008-07-01  
解决了,参考 http://blog.csdn.net/zhangyunbo1116/archive/2007/04/29/1589684.aspx

具体如下:

页面改为
var p = $('theId').value;
p = encodeURI(p);
p = encodeURI(p);//做两次,很重要
...

程序:
参数接收后,String ss = java.net.URLDecoder.decode(theId, "UTF-8");
  相关解决方案