[英]How to pass data from Controller to view in J2ee with spring
我有一個帶有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
我已使用此代碼解決了此模式下的問題
<?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...."
行移至第一行,現在可以工作了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.