我有一个带有spring mvc的简单J2ee应用程序。现在我想调用一个从jsp页面到他的控制器的方法,我想从控制器获得一个信息。所以我写了这段代码:
registrazione.jsp:
<script language="javascript">
function saveRigaTable2(){
try{
var nome = document.getElementById("nome").value;
var ajaxHandler = new AjaxHandler('POST', '<c:url value="${pageName}"/>.html', false);
ajaxHandler.encodeParameter('method', 'saveItems');
ajaxHandler.encodeParameter('nome', nome);
ajaxHandler.callbackHandler = function()
{
if (ajaxHandler.getRequest().readyState == 4)
{
if (ajaxHandler.getRequest().status == 200)
{
var xmlDoc = ajaxHandler.getRequest().responseXML;
var itemsException = xmlDoc.getElementsByTagName("exception");
alert(itemsException.length);
var items = xmlDoc.getElementsByTagName("registrazioneOK");
alert(items.length);
if (itemsException.length!=0)
{
var punto=itemsException[0];
alert(punto.getAttribute("text"));
document.getElementById("error").innerHTML=punto.getAttribute("text");
alert('Impossibile1 eseguire l\' operazione');
}
if (items.length!=0)
{
var punto=items[0];
alert(punto.getAttribute("text"));
}
}
}
};
ajaxHandler.startRequest();
}
catch (e)
{
alert('Impossibile eseguire l\' operazione');
}
}
</script>
这是Controller的方法:
public ModelAndView saveItems(HttpServletRequest request, HttpServletResponse response) throws Exception
{
List dataSet = new ArrayList();
Map dataItem = new HashMap();
List attributes = new ArrayList();
Map model=new HashMap();
try
{
String nome=request.getParameter("nome");
User user = new User();
user.setNome(nome);
if(modelManager.getUserManager().saveUser(user)){
//request.getSession().setAttribute("rilasciataModificataConf", "SI");
//registrazione avvenuta con successo
dataItem.put("tagName", "registrazioneOK");
attributes.add(new Entity("text", "Complimenti. Registrazione avvenuta con successo." ));
dataItem.put("attributes", attributes);
dataSet.add(dataItem);
model.put("dataSet", dataSet);
}
} catch (Exception e) {
dataItem.put("tagName", "exception");
attributes.add(new Entity("text", "Error in saving the current row" ));
dataItem.put("attributes", attributes);
dataSet.add(dataItem);
model.put("dataSet", dataSet);
LoggerFactory.logStackTrace(e);
}
request.setAttribute("model", model);
return new ModelAndView("secure/ajax_callback", model);
}
这是ajax_callback.jsp
<%@ page contentType="text/xml"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<?xml version="1.0" encoding="iso-8859-1"?>
<dataSet>
<c:forEach var="dataItem" items="${dataSet}">
<<c:out value="${dataItem.tagName}"/>
<c:forEach var="attribute" items="${dataItem.attributes}">
<c:out value="${attribute.id}"/>="<c:out value='${attribute.description}'/>"
</c:forEach>/>
</c:forEach>
</dataSet>
当我运行此代码时,我无法读取信息,项目的长度始终为0。 我的错误在哪里?
编辑:这是我从ajax_callback.jsp获取的xml:
<?xml version="1.0" encoding="UTF-8"?>
<dataSet>
<
exception
text="Complimenti. Registrazione avvenuta con successo."
/>
这是xml的错误
Errore interpretazione XML: la dichiarazione XML o testuale non è all’inizio di un’entità Indirizzo: moz-nullprincipal:{d84efa50-0bb7-4f19-b93a-de7ebc15f366} Riga numero 3, colonna 1:
<?xml version="1.0" encoding="UTF-8"?>
^
我认为错误发生在ajax_callback.jsp
上答案 0 :(得分:0)
我已在此模式下修复了问题(使用此代码)
<?xml version="1.0" encoding="UTF-8"?>
<%@ page contentType="text/xml"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<dataSet>
<c:forEach var="dataItem" items="${dataSet}">
<<c:out value="${dataItem.tagName}"/>
<c:forEach var="attribute" items="${dataItem.attributes}">
<c:out value="${attribute.id}"/>="<c:out value='${attribute.description}'/>"
</c:forEach>/>
</c:forEach>
</dataSet>
我已将"<?xml...."
的行移至第一行,现在可以正常工作