[英]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.