Java 如何在jqueryajax成功方法中调用spring控制器
我创建了一个spring控制器和一个jsp页面。在jsp页面中,我使用jQueryAjax调用来点击控制器。现在,该控制器以字符串形式返回json响应。现在,基于成功方法中的json响应,我想调用下一个控制器调用,该调用将返回ModelAndView jsp页面。我怎样才能做到这一点。下面是我的代码: JSP Jquery ajax调用:Java 如何在jqueryajax成功方法中调用spring控制器,java,jquery,ajax,spring,jsp,Java,Jquery,Ajax,Spring,Jsp,我创建了一个spring控制器和一个jsp页面。在jsp页面中,我使用jQueryAjax调用来点击控制器。现在,该控制器以字符串形式返回json响应。现在,基于成功方法中的json响应,我想调用下一个控制器调用,该调用将返回ModelAndView jsp页面。我怎样才能做到这一点。下面是我的代码: JSP Jquery ajax调用: $(document).ready(function(){ $("#submitButton").click(function(e){
$(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();
}
如何执行此操作。由于第二个处理程序方法返回
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");
}
这就是您获取url的方式。如果要加载其他页面或创建ajax调用,则可以这样做
Map<String, String> map = new HashMap<String, String>();
if(condition1){
map.put("url","url1.html");
}
if(condition2){
map.put("url","url2.html");
}
success:function(jsonStr){
var obj = JSON.parse(jsonStr);
var url = obj.url;
}