java - 如何在 Spring MVC/WebFlow 项目中进行 AJAX 调用

标签 java ajax spring spring-mvc spring-webflow

我想从 SWF 流中的 View 对 Spring MVC Controller 进行 AJAX 调用。我需要在 portlet 容器内而不是在 portlet session 之外的 servlet 中执行此操作。

我试过使用 <portlet:resourceURL id="myAjax"/>并使用 @ResourceMapping在 Spring Controller 类中,但这会干扰 SWF FlowHandler,因为它试图处理请求。

我想做的事可行吗?如果是这样,有人对如何做有任何建议吗?

谢谢。

最佳答案

在我的 view-state1 spring web flow 中我有这个:

<script type="text/javascript">
        function getInfoPersona(tipoSearch) {
            dojo.require("dojox.widget.Standby");

            if(tipoSearch == 'cedula'){
                var param = document.getElementById('cedula').value;
                if(!param){
                    return alert('Escriba una cédula');
                }
            }else{
                var param = document.getElementById('numero').value;
                if(!param){
                    return alert('Escriba un número');
                }               
            }
           var url = "/application/personae/info/"+tipoSearch+"/"+param+".json";            

           var standby = new dojox.widget.Standby({target: "loadingInfo"});
           document.body.appendChild(standby.domNode);
           standby.startup();           
            standby.show();
            dojo.xhrGet({
            // URL del recurso en el servidor
            url: url, 
            handleAs: "json",
            timeout: 5000, // Time in milliseconds

            // The LOAD function will be called on a successful response.
            load: function(response, ioArgs) { //
                if(response.nombre){
                    document.getElementById('infoPersona').innerHTML = '<ul>'+
                        //'<li>Estatus: '+response.estatus+'</li>'+         
                        '<li>Nombre: '+response.nombre+'</li>'+
                                '<li>Centro: '+response.centro+'</li></ul>';

                        if(tipoSearch == "cedula"){
                            if( response.numero > 0){
                                document.getElementById("numero").value = response.numero;
                                //document.getElementById("numero").readOnly = true;
                                document.getElementById("buttonSearchNumero").disabled = true;
                            }else{
                                //document.getElementById("numero").value = '';
                                document.getElementById("numero").readOnly = false;
                                document.getElementById("buttonSearchNumero").disabled = true;                              
                            }
                        }

                        if(tipoSearch == "numero"){
                            document.getElementById("cedula").value = response.cedula;
                            document.getElementById("cedula").readOnly = true;
                            document.getElementById("buttonSearchCedula").disabled = true;
                        }

                        if(response.ambito != ''){
                            document.getElementById('ambito').value = response.ambito;  
                        }


                        document.getElementById('twitter').value = response.twitter;

                        document.getElementById('correo').value = response.email;                           

                        document.getElementById('numeroTelefono').value = response.telefono;

                        if(response.parroquiaId > 0){
                            seleccionarMunicipioParroquia(response.parroquiaId, response.municipioId);                              
                        }


                        document.getElementById('infoPersona').style.visibility = 'visible';    
                        document.getElementById('success').disabled = false;
                }else{
                    if(tipoSearch == 'cedula'){
                        document.getElementById('infoPersona').innerHTML = 'No se encontraron datos';
                        document.getElementById('success').disabled = true;
                    }else{
                        document.getElementById('infoPersona').innerHTML = 'No se encontraron datos para el número de escrito';                     
                    }    
                    document.getElementById("buttonSearchCedula").disabled = false;
                    document.getElementById("buttonSearchNumero").disabled = false;
                }
                standby.hide();  
                document.getElementById('infoPersona').style.visibility = 'visible';    

            },

            // The ERROR function will be called in an error case.
            error: function(response, ioArgs) { // 
              console.error("HTTP STATUS CODE: ", ioArgs.xhr.status); //
              dojo.byId("infoPersona").innerHTML = 'No se pudo cargar la información básica de la persona'; //
              standby.hide();
              document.getElementById('infoPersona').style.visibility = 'visible';            
              return response; // 
              }
            }); 
          }
      </script> 

和我的 Controller :

@RequestMapping(value = "/info/cedula/{cedula}.json", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public InfoPersona getPersonaAsJson(@PathVariable("cedula") Integer cedula) {

        InfoPersona te = new InfoPersona();
        Persona v = Persona.findPersonaByCedulaEquals(cedula);

        if(v == null){
            return te;
        }


        setDataPersona(v.getPersonaId(), te);

        te.setTipoBusqueda("cedula");

        if(!telefonosString.equals("")){
            //eliminar la coma final
            telefonosString = telefonosString.substring(0, telefonosString.length() - 1);
            te.setAllTelefonos(telefonosString);
        }

        te.setTelefonos(numeros);

        return te;
    }

关于java - 如何在 Spring MVC/WebFlow 项目中进行 AJAX 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17081767/

相关文章:

html - 如何将responseText转换成json

javascript - Ajax 调用获取远程文件仅返回文件的一部分

java - 如何在 Spring Boot 应用程序中实现安全性?

java - 使用 Jackson 和 JavaTimeModule 序列化 Java 8 ZonedDateTime

java - JSF 中的数据表

java - 在 myeclipse 中安装 windowBuilder

java - 如何使用 selenium webdriver 在滚动的动态加载网格中搜索元素?

java - 无法配置 Spring MVC 应用程序

spring - 使用 Spring 3 验证的两种方法?

java - 如何在 Mac OS X 上使用 Ant 复制 .​​app 包?