[英]Cannot bind data to ModelAttribute list properties in spring mvc
我正在使用 spring mvc 和 ajax 從服務器請求數據
這是我的 ModelAttribute class
@Data
public class PromotionSettingCriteria extends BaseRequest{
private Long[] promotionIds;
private Long promotionId;
}
這是我的 ajax 請求
$.ajax({
url: path + '/promotion/setting/search.htm',
type: 'POST',
data: {
promotionIds: promotionIds,
promotionId: promotionIds[0],
},
success: function (response) {
let settingResponse = JSON.parse(response);
console.log('Promotion setting', response);
if (settingResponse.status == '1') {
// console.log(response)
}
},
error: function () {
console.log("Promotion setting error");
}
})
Controller class
@Controller
@RequestMapping("/promotion")
public class PromotionController extends BaseController {
@RequestMapping(value = "/setting/search", method = RequestMethod.POST)
@ResponseBody
public Object searchPromotionSetting(HttpServletRequest request,@ModelAttribute PromotionSettingCriteria criteria) {
try{
Map<String, Object> requestParams = getRequestParams(request);
if (criteria != null && criteria.getPromotionIds() == null){
throw new ServiceException("Promotion Setting Criteria Cannot be NULL");
}
List<PromotionSetting> resultData = promotionSettingService.getPromotionSettingByCriteria(criteria);
return RequestUtil.createSuccessResponse(resultData);
}catch (Exception e){
return RequestUtil.createFailResponse(e);
}
}
}
當我沒有傳遞promotionIds 時,controller 工作正常並綁定promotionId 屬性但是當我傳遞promotionIds 時它顯示java.lang.NumberFormatException: For input string: ""
[analyze][DEBUG] [2020-07-31 17:35:18] org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(134) | Resolving exception from handler [com.analyze.controller.PromotionController@1af465c7]: java.lang.NumberFormatException: For input string: ""
[analyze][DEBUG] [2020-07-31 17:35:18] org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(134) | Resolving exception from handler [com.analyze.controller.PromotionController@1af465c7]: java.lang.NumberFormatException: For input string: ""
[analyze][DEBUG] [2020-07-31 17:35:18] org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(134) | Resolving exception from handler [com.analyze.controller.PromotionController@1af465c7]: java.lang.NumberFormatException: For input string: ""
[analyze][DEBUG] [2020-07-31 17:35:18] org.springframework.web.servlet.FrameworkServlet.processRequest(989) | Could not complete request
java.lang.NumberFormatException: For input string: ""
如何讓 controller 綁定 ModelAttribute class 中的列表屬性?
更新您的 ajax 請求
$.ajax({
url: path + '/promotion/setting/search.htm',
type: 'POST',
contentType: "application/json",
data: JSON.stringify({
promotionIds: promotionIds,
promotionId: promotionIds[0],
}),
dataType: "json",
success: function (response) {
let settingResponse = JSON.parse(response);
console.log('Promotion setting', response);
if (settingResponse.status == '1') {
// console.log(response)
}
},
error: function () {
console.log("Promotion setting error");
}
})
此外,您需要更新您的 Controller 方法,將@ModelAttribute
替換為@RequestBody
@RequestMapping(value = "/setting/search", method = RequestMethod.POST)
@ResponseBody
public Object searchPromotionSetting(HttpServletRequest request, @RequestBody PromotionSettingCriteria criteria) {
// your code here
}
content-type: "application/json"
表示你要發送 jsondataType: "json"
您期望 json 作為響應data: JSON.stringify({...})
將您的 js object 轉換為字符串
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.