52,797
社区成员
发帖
与我相关
我的任务
分享
public ActionForward responseAjaxRequest(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
{
response.setContentType("text/xml;charset=gb2312");
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("<?xml version='1.0' encoding='gb2312'?>");
stringBuffer.append("\n");
stringBuffer.append("<books>");
stringBuffer.append("\n");
stringBuffer.append("<book>Struts开发基础</book>");
stringBuffer.append("\n");
stringBuffer.append("<book>Hibernate开发基础</book>");
stringBuffer.append("\n");
stringBuffer.append("<book>Spring开发基础</book>");
stringBuffer.append("\n");
stringBuffer.append("</books>");
String xmlFile = stringBuffer.toString();
PrintWriter printWriter = response.getWriter();
printWriter.write(xmlFile);
printWriter.flush();
printWriter.close();
return null;
}
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("<?xml version='1.0' encoding='UTF-8'?>");
stringBuffer.append("root");
stringBuffer.append("<book>Java开发基础</book>");
stringBuffer.append("<book>Java开发基础</book>");
stringBuffer.append("<book>Java开发基础</book>");
stringBuffer.append("/root");
String xmlFile = stringBuffer.toString();
response.setHeader("Cache-Control ", "no-cache");
response.setContentType("text/xml;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter printWriter = response.getWriter();
printWriter.write(xmlFile);
printWriter.flush();
printWriter.close();
return null;
if (xmlHttp == null)
{
// 创建 xmlHttp 对象
createXMLHttpRequest();
}
// 通过timestamp="+new Date().getTime();使得每次发送的URL不同,强制浏览器刷新
var url = "site.ajax.linkage.do?method=responseCompRequest×tamp=" + new Date().getTime();
xmlHttp.open("post", url, true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
xmlHttp.onreadystatechange = function(){deptListCallback(compID);};
xmlHttp.send(null);
function deptListCallback(id)
{
if (xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
processServerResponseForDeptMessage();
}
}
}
function processServerResponseForDeptMessage()
{
var xmlRead = xmlHttp.responseXML();
alert("xmlRead" + xmlRead);
}
var req;
function Change_Select() {
var url = "";
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if (req) {
req.open("GET", url, true);
req.onreadystatechange = callback;
req.send(null);
}
}
function callback() {
if (req.readyState == 4) {
if (req.status == 200) {
parseMessage();
} else {
alert("Not able to retrieve description" + req.statusText);
}
}
}
function parseMessage() {
var xmlDoc = req.responseXML;
}
stringBuffer.append("<?xml version='1.0' encoding='gb2312'?>");
很可能是这里有错误,encoding='gb2312'你设成utf-8的试试看。