Spring mvc表单错误映射2个模型

时间:2017-04-30 14:21:45

标签: java spring hibernate spring-mvc

当我尝试从url打开创建表单时我遇到了错误。我有2个模型被映射为一对多并使用autopopulatinglist:

ContractHeader.java

sudo -s -- <<...

ContractEntitlement.java

@Entity
@Table(name = "CONTRACT_HEADER")
public class ContractHeader {

    @Id
    @Column(name = "id", unique = true, nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "contractHeader")
    private List<ContractEntitlement> contractEntitlements;

    @Column(name = "customer_name")
    private String customerName;

    getter and setter ....
}

控制器

@Entity
@Table(name = "CONTRACT_ENTITLEMENT")
public class ContractEntitlement {

    @Id
    @Column(name = "id", unique = true, nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id", nullable = false, insertable = false, updatable = false)
    private ContractHeader contractHeader; 

    getter and setter ....
}

Create.jsp

@RequestMapping(value = "/create", method = RequestMethod.GET)
public String createForm(Model model) {
    ContractHeader ch = new ContractHeader();
    ch.setContractEntitlements(new AutoPopulatingList<ContractEntitlement>(ContractEntitlement.class));

    model.addAttribute("createForm", ch);

    return "create";
} 

错误:

<form:form method="post" commandName="createForm" action="${addAction}">
<table>
        <tr>
        <td>                  
            <form:input path="customerName" size="40" />
        </td>
        </tr>
</table>
</form:form>

我不明白为什么当我的输入路径对象来自ContractHeader类时会说contractEntitlement。

我的hibernate配置

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'contractEntitlement' available as request attribute

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'contractEntitlement' available as request attribute

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

原因在于我的jsp我注释掉了:

<!--    <td>
        <form:input name="category" type="text" path="contractEntitlement.category"/>
        </td>  -->

但由于某种原因,春天仍会检查它并抛出错误。删除代码的pirece修复它。我想知道为什么因为它出来了。无论如何这解决了我的问题,现在页面正在正确加载。