今天用百度编辑器遇到很奇怪的问题,百度编辑器用var=UE.getEditor('editor').getPlainTxt()获取内容经过ajax传回后台给struts的action处理就编程乱码了,而其它一般的input标签的就没问题的,弄了很久得出以下的解决方法。
1:在js文件中给百度编辑器的内容进行转码
<span style="font-size:18px;"><h3>function addArticle() {
/* 获取单选的值 */
var category = document.getElementsByName("category");
var value=0;
for ( var i = 0; i < category.length; i++) {
if (category[i].checked == true) {
value = category[i].value;
break;
}
}
var title=document.getElementById("titleTopic").value;
/* 获得百度编辑器的内容 */
var ue = UE.getEditor('editor');
var arr = [];
arr.push(UE.getEditor('editor').getPlainTxt());
alert(arr);
//var content=encodeURI(encodeURI(arr));
$.ajax({
url : "article/addArticle.action" +'?categoryID=' + value,type : 'post',dataType : 'json',data : {
"article.tiTle":title,"article.content":encodeURI(arr),},success : function(data) {
if (data[0].result == 'success') {
alert("成功发布博客");
window.location.href = "/BlogToyo/pages/mainPage.jsp";
} else {
alert("发布失败,请刷新页面重试");
}
}
});
}</h3></span>
2:在action后台则是解码
try {
content =URLDecoder.decode(article.getContent(),"utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
<span style="font-size:18px;color:#FF0000;">3:tomcat的server.xml配置(采用UTF-8编码)</span>
<pre name="code" class="html"><span style="font-size:18px;"> <Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8"</span>
<strong><span style="color:#FF0000;"> /></span></strong>
4:Struts的struts.xml配置(采用UTF-8编码)
<span style="font-size:18px;"> <!-- 指定web引用的默认编码集,相当于调用HttpServletRequest的setcharacterEncoding()方法 -->
<constant name="struts.i18n.encoding" value="UTF-8" /></span>
总结:jsp中乱码的问题不能小觑,要耐得住寂寞慢慢找出原因。
原文链接:https://www.f2er.com/ajax/164500.html