更新:
可能有些人正在寻找一种方法将JSON对象作为ajax参数传递,然后在spring控制器中转换为POJO。 如果他们使用请求方法GET,其他人可能会收到400 Bad Request错误。请在ajax和spring controller中使用POST类型。
var PaxTypeFare = function(paxType, baseFare, totalFeesAndTaxes, totalAmount) {
this.paxType = paxType;
this.baseFare = baseFare;
this.totalFeesAndTaxes = totalFeesAndTaxes;
this.totalAmount = totalAmount;
}
var SpaceShipTripInfo = function(from, to, fromSchedule, toSchedule){
this.from = from;
this.to = to;
this.fromSchedule = fromSchedule;
this.toSchedule = toSchedule;
};
...
var tInfos = new Array();
tInfos.push(new SpaceShipTripInfo("EARTH", "MOON", "2015-12-21T04:30:00", "2015-12-21T06:50:00"));
tInfos.push(new SpaceShipTripInfo("MOON", "MARS", "2015-12-21T03:30:00", "2015-12-21T011:10:00"));
tInfos.push(new SpaceShipTripInfo("VENUS", "KEPLER", "2015-12-21T01:30:00", "2015-12-21T22:30:00"));
tInfos.push(new SpaceShipTripInfo("EARTH", "SUN", "2015-12-20T02:30:00", "2015-12-29T15:10:00"));
var adultFares = new PaxTypeFare("ADT", 1000, 300, 1300);
var childFares = new PaxTypeFare("CHD", 750, 250, 1000);
var infantFares = null;
var data = {
tripInfos: tInfos,
adultFares: adultFares,
childFares: childFares,
infantFares: infantFares,
adultCount: 1,
childCount: 1,
infantCount: 0
};
var url = "/getSelectedTripPrices";
$.ajax({
url: url,
data: JSON.stringify(data),
contentType: "application/json",
type: "POST",
success: function (strHtml) {
//...
}
});
{
"tripInfos":[
{
"from":"EARTH",
"to":"MOON",
"fromSchedule":"2015-12-21T04:30:00",
"toSchedule":"2015-12-21T06:50:00"
},
{
"from":"MOON",
"to":"MARS",
"fromSchedule":"2015-12-21T03:30:00",
"toSchedule":"2015-12-21T011:10:00"
},
{
"from":"VENUS",
"to":"KEPLER",
"fromSchedule":"2015-12-21T01:30:00",
"toSchedule":"2015-12-21T22:30:00"
},
{
"from":"EARTH",
"to":"SUN",
"fromSchedule":"2015-12-20T02:30:00",
"toSchedule":"2015-12-29T15:10:00"
}
],
"adultFares":{
"paxType":"ADT",
"baseFare":"1000",
"totalFeesAndTaxes":"300",
"totalAmount":"1300.00"
},
"childFares":{
"paxType":"CHD",
"baseFare":"750",
"totalFeesAndTaxes":"250",
"totalAmount":"1000.00"
},
"infantFares": null,
"adultCount":"1",
"childCount":"1",
"infantCount":"0"
}
public Class ItineraryPrice {
private List<ItineraryPrice.SpaceShipTripInfo> tripInfos;
private ItineraryPrice.PaxTypeFare adultFares;
private ItineraryPrice.PaxTypeFare childFares;
private ItineraryPrice.PaxTypeFare infantFares;
private int adultCount;
private int childCount;
private int infantCount;
//Getters and Setters here
public static class SpaceShipTripInfo {
private String from;
private String to;
private String fromSchedule;
private String toSchedule
//Getters and Setters here
}
public static class PaxTypeFare {
private String paxType;
private Double baseFare;
private Double totalFeesAndTaxes;
private Double totalAmount;
//Getters and Setters here
}
}
@RequestMapping(value = "/getSelectedTripPrices", method = RequestMethod.POST, consumes = "application/json")
@ResponseBody
public ModelAndView getSelectedTripPrices(HttpSession session,
@RequestBody ItineraryPrice itineraryPrice,
BindingResult bindingResult) {
if(null != itineraryPrice.getAdultFares()){
System.out.println("[Type-code]: " + itineraryPrice.getAdultFares().getPaxType());
System.out.println("[Type-baseFare]: " + itineraryPrice.getAdultFares().getBaseFare());
System.out.println("[Type-totalFeesAndTaxes]: " + itineraryPrice.getAdultFares().getTotalFeesAndTaxes());
System.out.println("[Type-totalAmount]: " + itineraryPrice.getAdultFares().getTotalAmount());
}
else
System.out.println("ADULT-FARES-NULL");
if(null != itineraryPrice.getChildFares()){
System.out.println("[Type-code]: " + itineraryPrice.getChildFares().getPaxType());
System.out.println("[Type-baseFare]: " + itineraryPrice.getChildFares().getBaseFare());
System.out.println("[Type-totalFeesAndTaxes]: " + itineraryPrice.getChildFares().getTotalFeesAndTaxes());
System.out.println("[Type-totalAmount]: " + itineraryPrice.getChildFares().getTotalAmount());
}
else
System.out.println("CHILD-FARES-NULL");
if(null != itineraryPrice.getInfantFares()){
System.out.println("[Type-code]: " + itineraryPrice.getInfantFares().getPaxType());
System.out.println("[Type-baseFare]: " + itineraryPrice.getInfantFares().getBaseFare());
System.out.println("[Type-totalFeesAndTaxes]: " + itineraryPrice.getInfantFares().getTotalFeesAndTaxes());
System.out.println("[Type-totalAmount]: " + itineraryPrice.getInfantFares().getTotalAmount());
}
else
System.out.println("INFANT-FARES-NULL");
}
答案 0 :(得分:0)
您需要将这些数据转换为JSON对象: 要将信息发送到RequestParam,您可以使用:
$.ajax({
url: './controller/find.htm',
data: {
adultCount: 1,
childCount: 2,
childCount: 3
},
success: function (data) {
console.log('response=', data);
}
});
ModelAttribute用于macth表单变量,你需要使用RequestBody,你需要在jquery中创建一个Json对象,并在代码的数据部分添加它。