我创建了一个弹簧控制器和一个jsp页面。在jsp页面中,我使用jquery ajax调用来命中控制器。现在,此控制器以字符串形式返回json响应。现在基于json响应成功方法,我想调用下一个控制器调用,它将返回一个ModelAndView jsp页面。我怎样才能做到这一点。以下是我的代码:
JSP Jquery ajax调用:
Manifest-Version: 1.0
Class-Path: .
Main-Class: gui_rec.Main
控制器类方法:
$(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);
}
}); }
});
});
我想从jquery成功方法调用的第二个Controller方法:
@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();
}
我该怎么做。
答案 0 :(得分:2)
由于第二个处理程序方法返回ModelAndView
,因此您应该从成功回调中重定向:
...
success: function(response) {
window.location.replace(response.url);
}
...
答案 1 :(得分:1)
在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调用,那么您可以这样做。