我有一个应用程序,我将 ModelAndView 与 JQWidget 结合使用,AJAX 调用控制器进行插入/更新。
我的问题是 Update AJAX 调用没有调用控制器中的更新方法,它实际上调用了显示 MVC 表单的方法。
在不同版本的浏览器上它可以工作,但在某些时候 /forecast/update.do 被称为带有 Requestmapping edit.view 的控制器方法,即调用 updateForecastForm 而不是 updateForecast
我正在通过下面的代码进行解释。
谢谢,维姬
AddFore.js 的一部分
$(document).ready(function(){
theme = getDemoTheme();
// Get Current Date & User
var currentUser = "AUSER";
/* Edit a Customer - ed_save button event - Start */
var editSave = function(){
// alert(" Updating");
$("#ed_save").attr("disabled","disabled");
$("#waitText4").text('Please wait ... ');
$("#ed_save").css('cursor','progress');
var arr = [];
var contactRows = rowCount; // $(".ed_temp_contact").length;
// alert("ss " + contactRows);
for (var count=1; count<=contactRows; count++){ // count=0 is changed to count=1 because blank row which comes by default is deleted
var forecastdetail = {
productId: $("#rowList"+count+" [id~='productId']").val(),
forecastDate: new Date($("#rowList"+count+" [id~='forecastDate']").val()),
qty: $("#rowList"+count+" [name~='qty']").val(),
price: $("#rowList"+count+" [name~='price']").val(),
status: $("#status").val()
};
arr.push(forecastdetail);
}
var poDate = new Date($("#poYear").val(), $("#poMonth").val()-1, '01');
var addToCall = new Date($("#callYear").val(), $("#callMonth").val()-1, '01');
var formElements = new Object({
forecastId: $("#forecastId").val(),
forecastTitle: $("#forecastTitle").val(),
stage: $("#stage").val(),
customerId: $("#customer").val(),
lanWan: 'N',
notes: $("#notes").val(),
poDate: poDate,
dateAddedToCall: addToCall,
lastReviewed: currentDate,
status: $("#status").val(),
createdBy: $("#createdBy").val(),
createdOn: new Date($("#createdOn").val()),
updatedBy: currentUser,
updatedOn: currentDate,
forecastDetails: arr
});
var updateURL = contextPath+'/forecast/update.do';
alert(" Updating Opportunity * " + updateURL);
$.ajax({
type: 'POST',
url: updateURL,
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(formElements),
success : function(data, textStatus, jqXHR) {
alert(" Opportunity Updated.");
$("#ed_save").removeAttr("disabled","disabled");
$("#waitText4").text('Save successful...');
hideAndFade("#waitText4");
$("#ed_save").css('cursor','pointer');
$("#popupWindow").jqxWindow('hide');
// fetchAllRecords();
},
error : function(jqXHR, textStatus, errorThrown) {
// alert(" Opportunity Not Updated.");
console.log('error:', errorThrown);
$("#ed_save").removeAttr("disabled","disabled");
$("#waitText4").text('Save failed : '+errorThrown);
hideAndFade("#waitText4");
$("#ed_save").css('cursor','pointer');
}
});
};
现在控制器:
package com.tantalus.salesfunnel.controller;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.tantalus.salesfunnel.model.FCustomer;
import com.tantalus.salesfunnel.model.Forecast;
import com.tantalus.salesfunnel.model.ForecastReference;
import com.tantalus.salesfunnel.model.FProduct;
import com.tantalus.salesfunnel.model.ResponseObject;
import com.tantalus.salesfunnel.service.ICustomerService;
import com.tantalus.salesfunnel.service.IForecastService;
import com.tantalus.salesfunnel.service.IProductService;
import com.tantalus.salesfunnel.service.IReferenceService;
@Controller
public class ForecastController {
Logger logger = Logger.getLogger(ForecastController.class);
private IForecastService forecastService;
private ICustomerService customerService;
private IProductService productService;
private IReferenceService refService;
public IForecastService getIForecastService() {
return forecastService;
}
@Autowired
@Required
public void setForecastService(IForecastService forecastService) {
this.forecastService = forecastService;
}
public ICustomerService getCustomerService() {
return customerService;
}
@Autowired
@Required
public void setCustomerService(ICustomerService customerService) {
this.customerService = customerService;
}
public IProductService getProductService() {
return productService;
}
@Autowired
@Required
public void setProductService(IProductService productService) {
this.productService = productService;
}
public IReferenceService getRefService() {
return refService;
}
@Autowired
@Required
public void setRefService(IReferenceService refService) {
this.refService = refService;
}
@RequestMapping(value="/forecast/add.view", method= {RequestMethod.POST, RequestMethod.GET}, headers="Accept=application/json")
public ModelAndView addForecastForm () throws Exception {
System.out.println(" Inside /forecast/add.view ");
logger.debug("Inside addForecast.view");
ModelAndView mv = new ModelAndView("AddForecast");
Forecast f = new Forecast(); // forecastService.getForecast(customerName);
mv.addObject("AddForecast", f);
doCommon(mv);
return mv;
}
@RequestMapping(value="/forecast/add.do", method = {RequestMethod.POST, RequestMethod.GET})
@ResponseBody
public boolean addForecast(@RequestBody Forecast forecast) throws Exception {
System.out.println(" Inside /forecast/add.do ");
forecastService.addForecast(forecast);
return true;
}
@RequestMapping(value="/forecast/edit.view", method= {RequestMethod.POST, RequestMethod.GET}, headers="Accept=application/json")
public ModelAndView editForecast (@RequestParam(value="forecastId", required=false) int forecastId) {
System.out.println(" Inside /forecast/edit.view ");
logger.debug("Inside /forecast/edit.view");
ModelAndView mv = new ModelAndView("AddForecast");
Forecast forecast = new Forecast();
forecast = forecastService.getForecast(forecastId);
mv.addObject("forecast", forecast);
doCommon(mv);
return mv;
}
@RequestMapping(value="/forecast/search.view", method = RequestMethod.GET)
public @ResponseBody Forecast getForecast(@RequestParam("forecastId") int forecastId) {
System.out.println(" Inside search.view " + forecastId);
logger.info("Start search.view= "+ forecastId);
Forecast f = forecastService.getForecast(forecastId);
return f;
}
@RequestMapping(value="/forecast/update.do", method = {RequestMethod.POST, RequestMethod.GET})
@ResponseBody
public boolean updateForecast(@RequestBody Forecast forecast) throws Exception {
System.out.println(" Inside /forecast/update.do ");
forecastService.updateForecast(forecast);
return true;
}
@RequestMapping(value="/forecast/refresh.do", method=RequestMethod.GET)
@ResponseBody
public boolean refreshList() throws Exception {
forecastService.refreshList();
return true;
}
@RequestMapping(value="/forecast/list2.view", method=RequestMethod.GET)
public @ResponseBody ResponseObject<Forecast> listForecast2 () {
// logger.debug("Inside listForecasts");
ResponseObject<Forecast> forecasts = forecastService.getAllForecasts(0, 100);
return forecasts;
}
private void doCommon(ModelAndView mv) {
ResponseObject<FCustomer> customers = customerService.getAllCustomers(0, 100, 0);
List<FCustomer> custList = customers.getListOfModels();
mv.addObject("custList", custList);
List<FProduct> productList = productService.getProducts("");
mv.addObject("productList", productList);
List<ForecastReference> stageList = refService.getRefs("OPP_STAGE");
mv.addObject("stageList", stageList);
}
}