mercredi 18 février 2015

How to pass data from Controller to view in J2ee with spring

I have a simple J2ee application with spring mvc. Now I want to call a method from the jsp page to his controller and I want to have an information from the controller. So I have write this code:


registrazione.jsp



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>


This is the method of 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);
}


This is the ajax_callback.jsp



<%@ page contentType="text/xml"%>
<%@ taglib prefix="c" uri="http://ift.tt/QfKAz6"%>
<?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>


When I run this code, I can't read the information, the lenght of the items is all time 0. Where is my error?


Aucun commentaire:

Enregistrer un commentaire