繁体   English   中英

如何在jquery ajax成功方法中调用spring controller

[英]how to call spring controller in jquery ajax success method

我创建了一个弹簧控制器和一个jsp页面。 在jsp页面中,我使用jquery ajax调用来命中控制器。 现在,此控制器以字符串形式返回json响应。 现在基于json响应成功方法,我想调用下一个控制器调用,它将返回一个ModelAndView jsp页面。 我怎样才能做到这一点。 以下是我的代码:

JSP Jquery ajax调用:

$(document).ready(function(){
    $("#submitButton").click(function(e){
         var formData = getFormData();
         if(formData!=false){
         $.ajax({
            type: 'POST', 
            'url': 'http://localhost:8080/Test_ReportingUI/fieldMappingNext.htm',
            data: {jsonData: JSON.stringify(formData)},
            dataType: 'json',
            success: function(response){ 
                 try{
                    var strResponse=jQuery.parseJSON(response);
                }catch(err){}
                if(response.status=='ok')
                {
                    alert ("okokokokokokokokok");
                //I am successfully reaching till here. 
                //But in case of this alert box I want to call a 
                //controller which will return ModelAndView and 
                //should open a corresponding ModelAndView jsp page.
                //something like:
                /*
                $.ajax({
                type: 'GET', 
                'url': 'http://localhost:8080/Test_ReportingUI/abcxyz.htm',  
                )};
                */
                }
                else
                {
                    alert("ERROR!!");
                } 

            },
            timeout: 10000,
            error: function(xhr, status, err){ 
                if(response.status=='timeout')
                {   
                    alert('Request time has been out','');
                }
                console.log(status,err); 
            }
        }); }
     });
});

控制器类方法:

@RequestMapping (value="fieldMappingNext.htm", method=RequestMethod.POST)
@ResponseBody String addFieldMappingNext(@RequestParam String jsonData)
{
    String customerID =null;
    String objectID = null;
    String syncFieldName = null;
    String optMapping = null;
    JSONObject jsonResponse = new JSONObject();
    try{
        JSONObject requestedJSONObject = new JSONObject(jsonData);
        customerID = requestedJSONObject.getString("customerID");
        objectID = requestedJSONObject.getString("objectID");
        syncFieldName = requestedJSONObject.getString("syncFieldName");
        optMapping = requestedJSONObject.getString("optMapping");
    }catch(Exception exex){
        exex.printStackTrace();
    }
    if(optMapping.equalsIgnoreCase("direct")){
        long metadataID=rwCustomerService.getMetaDataID(customerID,objectID);
        List<RWFieldDetail> list=rwCustomerService.getFieldDetailNames(metadataID);
        request.setAttribute("customerID", customerID);
        request.setAttribute("objectID", objectID);
        request.setAttribute("optMapping", optMapping);
        request.setAttribute("syncFieldName", syncFieldName);
        request.setAttribute("fieldNames", list);
        try {
            jsonResponse.put("status", "ok");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return jsonResponse.toString();
}

我想从jquery成功方法调用的第二个Controller方法:

@RequestMapping (value="abcxyz.htm", method=RequestMethod.GET)
ModelAndView fieldMapping(){
ModelAndView modelAndView=new ModelAndView("FieldMappingMainScreenNext");
return modelAndView;
}

我该怎么做呢。

由于第二个处理程序方法返回ModelAndView ,因此您应该从成功回调重定向:

...

success: function(response) {
    window.location.replace(response.url);
}

...

在您的Java代码中,您可以使用以下内容:

Map<String, String> map = new HashMap<String, String>();
if(condition1){
   map.put("url","url1.html");
}
if(condition2){
   map.put("url","url2.html");
}

将其转换为JSON字符串并将其还原。 然后,在jquery部分中,您将得到响应:

success:function(jsonStr){
    var obj = JSON.parse(jsonStr);
    var url = obj.url;
}

这就是你如何获得网址。 如果要加载其他页面或创建ajax调用,则可以执行此操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM