【问题标题】:Spring MVC object passed with model attribute is empty使用模型属性传递的 Spring MVC 对象为空
【发布时间】:2012-03-18 09:01:36
【问题描述】:

@ModelAttribute 有问题,

在 GET 方法中,当我使用 getBankAcc() 时,我的客户有带有银行账户的数组列表,但是当我将对象“客户”从 GET 传递到 POST 时,他在 BankAcc 列表中有 [];/

我的代码片段如下:

    @RequestMapping(value = "/aaa.html", method = RequestMethod.GET)
public String aaaGet(Model m, Principal principal) {

...
    CustEntity cust = custService.getCustByUserName(principal);
    cust.getBankAcc();

    m.addAttribute("customer", cust);

...
}


@RequestMapping(value = "/aaa.html", method = RequestMethod.POST)
public String aaaPost(
        @ModelAttribute("customer") CustomerEntity cust,
        BindingResult results, RedirectAttributes redirectAttributes,
        Model m) {

    cust.getBankAcc();

    ...
}

问候, 风骚的

【问题讨论】:

    标签: java spring-mvc modelattribute


    【解决方案1】:

    在 aaaPost 中,CustomerEntity 客户将与表单中的数据绑定。 也就是说,aaaPost 中的 cust 不是您在 aaaGet 中放入模型的那个。

    【讨论】:

      【解决方案2】:

      我也遇到了这个问题并得到了解决方案:

      将弹簧表单添加到您的页面:

      <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> 
      

      并使用如下形式:

      <form:form action="/someUrl" modelAttribute="objectName" method="post">
           <form:input path="fieldName"/>
           <button type="submit">buttonName</button>
      </form:form>
      

      【讨论】:

        猜你喜欢
        • 2017-04-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-02
        • 1970-01-01
        相关资源
        最近更新 更多