尝试发出请求并将数据从Ajax传递到Spring Controller时, null数据映射到ModelAttribute。
Ajax Call:
var i = 0;
var pFirstName=$("#pFirstName").val();
var pLastName=$("#pLastName").val();
var pAge=$("#pAge").val();
var pEmail=$("#pEmail").val();
var pContactNo=$("#pContactNo").val();
var serviceType=[];
var clinicName=[];
var providerCity=[];
var insuranceType=[];
var procedureDate=[];
var providerFName=[];
var pPrice=[];
var totalPrice=[];
for (i=1; i<=addFeedbackMore; i++) {
if (!isEmptyOrBlank($("#serviceType"+i).val())) {
serviceType[i-1]=($("#serviceType"+i).val());
}
if (!isEmptyOrBlank($("#clinicName"+i).val())) {
clinicName[i-1]=($("#clinicName"+i).val());
}
if (!isEmptyOrBlank($("#providerCity"+i).val())) {
providerCity[i-1]=($("#providerCity"+i).val());
}
if (!isEmptyOrBlank($("#insuranceType"+i).val())) {
insuranceType[i-1]=($("#insuranceType"+i).val());
}
if (!isEmptyOrBlank($("#procedureDate"+i).val())) {
procedureDate[i-1]=($("#procedureDate"+i).val());
}
if (!isEmptyOrBlank($("#providerFName"+i).val())) {
providerFName[i-1]=($("#providerFName"+i).val());
}
if (!isEmptyOrBlank($("#pPrice"+i).val())) {
pPrice[i-1]=($("#pPrice"+i).val());
}
if (!isEmptyOrBlank($("#totalPrice"+i).val())) {
totalPrice[i-1]=($("#totalPrice"+i).val());
}
}
$.ajax({
type : "POST",
url : "/feedback/feedbackDeatils",
data : JSON.stringify({
"pFirstName":pFirstName,
"pLastName":pLastName,
"pAge":pAge,
"pEmail":pEmail,
"pContactNo":pContactNo,
"serviceType": serviceType,
"clinicName": clinicName,
"providerCity": providerCity,
"insuranceType": insuranceType,
"procedureDate": procedureDate,
"providerFName": providerFName,
"pPrice": pPrice,
"totalPrice": totalPrice
}),
contentType: 'application/json',
success : function(response) {
// do something ...
restricted = false;
restrictedFlag = 1;
$('#gridSystemModal').modal('hide');
},
error : function(e) {
alert('Error: ' + e);
}
});
Spring Controller:
@RequestMapping(value="/feedbackDeatils", method = {RequestMethod.GET, RequestMethod.POST})
public @ResponseBody List<Provider> getProviderList(@ModelAttribute FeedbackDetails feedbackData) throws ParseException {
feedbackService.insertFeedbackDetails(feedbackData);
return null;
}
Bean类: 公共类FeedbackDetails {
private String pFirstName;
private String pLastName;
private String pAge;
private String pEmail;
private String pContactNo;
private List<String> serviceType;
private List<String> clinicName;
private List<String> providerCity;
private List<String> insuranceType;
private List<String> procedureDate;
private List<String> providerFName;
private List<String> pPrice;
private List<String> totalPrice;
public String getPFirstName() {
return pFirstName;
}
public void setPFirstName(String pFirstName) {
this.pFirstName = pFirstName;
}
public String getPLastName() {
return pLastName;
}
public void setPLastName(String pLastName) {
this.pLastName = pLastName;
}
public String getPAge() {
return pAge;
}
public void setPAge(String pAge) {
this.pAge = pAge;
}
public String getPEmail() {
return pEmail;
}
public void setPEmail(String pEmail) {
this.pEmail = pEmail;
}
public String getPContactNo() {
return pContactNo;
}
public void setPContactNo(String pContactNo) {
this.pContactNo = pContactNo;
}
public List<String> getServiceType() {
return serviceType;
}
public void setServiceType(List<String> serviceType) {
this.serviceType = serviceType;
}
public List<String> getClinicName() {
return clinicName;
}
public void setClinicName(List<String> clinicName) {
this.clinicName = clinicName;
}
public List<String> getProviderCity() {
return providerCity;
}
public void setProviderCity(List<String> providerCity) {
this.providerCity = providerCity;
}
public List<String> getInsuranceType() {
return insuranceType;
}
public void setInsuranceType(List<String> insuranceType) {
this.insuranceType = insuranceType;
}
public List<String> getProcedureDate() {
return procedureDate;
}
public void setProcedureDate(List<String> procedureDate) {
this.procedureDate = procedureDate;
}
public List<String> getProviderFName() {
return providerFName;
}
public void setProviderFName(List<String> providerFName) {
this.providerFName = providerFName;
}
public List<String> getPPrice() {
return pPrice;
}
public void setPPrice(List<String> pPrice) {
this.pPrice = pPrice;
}
public List<String> getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(List<String> totalPrice) {
this.totalPrice = totalPrice;
}
}
在尝试获取数据时,获取NullPointerException。