【问题标题】:Spring MVC ModelAttribute values being lostSpring MVC ModelAttribute 值丢失
【发布时间】:2018-02-22 22:05:39
【问题描述】:

我遇到了我的模型属性在页面之间丢失值的情况。

我有两个控制器方法分别处理 GET 和 POST 请求。

GET方法

@RequestMapping(value = "/checkout/billing", method = RequestMethod.GET)
    public String getBillingPage(Model model, final HttpServletRequest request) throws CMSItemNotFoundException {
   // other code

   checkoutForm.setCustomFieldsForm(customFieldsForm);
   model.addAttribute("checkoutForm", checkoutForm);

   // other code
}

GET 方法完成后的调试视图

POST 方法

@RequestMapping(value = "/checkout/billing", method = RequestMethod.POST)
    public String submitPayment(
            @Valid @ModelAttribute("checkoutForm") final CheckoutForm checkoutForm,
            final BindingResult bindingResult,
            Model model,
            final HttpServletRequest request,
            final HttpServletResponse response) throws CMSItemNotFoundException 
    {}

调用 POST 方法时的调试视图

1234 来自用户在表单字段中输入该数据。其他值应该仍然存在并且不为空。

这里会发生什么?

【问题讨论】:

    标签: java spring model-view-controller


    【解决方案1】:

    您的模型未存储在会话中。每个请求都会创建一个新的模型对象。这就是为什么它是空的。

    您可以将模型添加为会话属性。请在此处找到文档https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-sessionattrib

    【讨论】:

      猜你喜欢
      • 2018-06-30
      • 2013-12-31
      • 1970-01-01
      • 1970-01-01
      • 2014-09-20
      • 1970-01-01
      • 2017-11-27
      • 2012-01-01
      相关资源
      最近更新 更多