java - 如何使用spring将数据从Controller传递到J2ee中查看

标签 java spring jsp

我有一个带有 spring mvc 的简单 J2ee 应用程序。现在我想从jsp页面调用一个方法到他的 Controller ,并且我想从 Controller 获得信息。所以我写了这段代码:

注册.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...." 行如何第一行现在工作

关于java - 如何使用spring将数据从Controller传递到J2ee中查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28585703/

相关文章:

java - 从嵌套 HashMap 中检索值

作为静态或原型(prototype)的 Java 实用方法

jsp - 如何将网络摄像头连接到jsp?

java - Android 假设 "main/UI thread"ID 始终为 1 是否安全?

java - 远程客户端NotBoundException

java - hibernate 二级缓存: Update cached collection on creating of entity in oneToMany relation

Java/Spring : How to specify order for nested transactions with TransactionSynchronizationManager

java - 服务器在向 Spring Controller 发出几次请求后挂起

java - 在自定义 JSP 标记中传递 Java 对象值

java - 我如何处理/限制用户访问 servlet 和 jsp?