【问题标题】:Cannot bind data to ModelAttribute list properties in spring mvc无法将数据绑定到 Spring mvc 中的 ModelAttribute 列表属性
【发布时间】:2020-11-21 19:49:57
【问题描述】:

我正在使用 spring mvc 和 ajax 从服务器请求数据

这是我的 ModelAttribute 类

@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
@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 时,控制器工作正常并绑定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: ""

如何让控制器绑定ModelAttribute类中的list属性?

【问题讨论】:

  • 试试@Responsebody 而不是@ModelAttribute
  • 你的意思是RequestBody吗?
  • 对不起,我的意思是@RequestBody
  • 我试过了,但是显示404

标签: java ajax spring spring-mvc


【解决方案1】:

更新您的 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");
    }
})

另外,您需要更新控制器方法,将@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"你要发送json的指标
  • dataType: "json" 你期待 json 作为响应
  • data : JSON.stringify({...}) 将您的 js 对象转换为字符串

【讨论】:

    猜你喜欢
    • 2013-01-16
    • 2020-06-11
    • 1970-01-01
    • 2016-08-15
    • 2016-05-26
    • 2017-11-27
    • 2019-04-01
    • 2018-12-22
    • 2012-06-15
    相关资源
    最近更新 更多