gpt4 book ai didi

java - 嵌套对象空字段的 Spring 表单绑定(bind)问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:36:31 25 4
gpt4 key购买 nike

我在使用 spring @ModelAttribute 表单绑定(bind)时遇到了一个相当尴尬的问题。我有以下实体类,它在表单绑定(bind)中用作策略类的嵌套类。

@javax.persistence.Entity
@Table(name = "entity")
@XmlRootElement
public class Entity implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "entity_id")
private Integer entityId;

@Basic(optional = false)
@NotNull
@Column(name = "entity_type")
private int entityType;

@Basic(optional = false)
@NotNull
@Size(min = 1, max = 2147483647)
@Column(name = "entity_name")
private String entityName;

public Entity() {
}

public Entity(Integer entityId) {
this.entityId = entityId;
}

public Entity(Integer entityId, int entityType, String entityName) {
this.entityId = entityId;
this.entityType = entityType;
this.entityName = entityName;
}

public Integer getEntityId() {
return entityId;
}

public void setEntityId(Integer entityId) {
this.entityId = entityId;
}

public int getEntityType() {
return entityType;
}

public void setEntityType(int entityType) {
this.entityType = entityType;
}

public String getEntityName() {
return entityName;
}

public void setEntityName(String entityName) {
this.entityName = entityName;
}
}

我的问题的主要兴趣是 Policy 类,它是顶级类和主要绑定(bind),如下所示:

@Entity
@Table(name = "policy")
@XmlRootElement
public class Policy implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "policy_id")
private Integer policyId;

@Basic(optional = false)
@NotNull
@Size(min = 1, max = 2147483647)
@Column(name = "reference_number")
private String referenceNumber;

@JoinColumn(name = "adjuster_id", referencedColumnName = "entity_id", nullable = true)
@ManyToOne(optional = true)
private Entity adjusterId;

public Policy() {
}

public Policy(Integer policyId) {
this.policyId = policyId;
}

public Integer getPolicyId() {
return policyId;
}

public void setPolicyId(Integer policyId) {
this.policyId = policyId;
}

public String getReferenceNumber() {
return referenceNumber;
}

public void setReferenceNumber(String referenceNumber) {
this.referenceNumber = referenceNumber;
}


public Entity getAdjusterId() {
return adjusterId;
}

public void setAdjusterId(Entity adjusterId) {
this.adjusterId = adjusterId;
}
}

这是 Controller 方法,它从 View 绑定(bind)中获取带注释的 ModelAttribute Policy 参数。

@RequestMapping(value = "/create", method = RequestMethod.POST)
public ModelAndView create(@ModelAttribute Policy p) {
policyService.create(p);
return new ModelAndView("viewName");
}

最后,这是代码的表单/ View 部分:

<form id="policyInformationForm" role="form" th:object="${policy}" th:action="@{${mode == 'CREATE'} ? '/policy/create' : '/policy/update'}" method="post">
<div class="box-body">
<div class="form-group">
<label for="policyId">Policy Id</label>
<input type="text" th:field="*{policyId}" class="form-control" id="policyId" placeholder="" readonly="readonly" />
</div>

<div class="form-group">
<label for="referenceNumber">Policy Number (May change while saving!!!)</label>
<input type="text" th:field="*{referenceNumber}" class="form-control" id="referenceNumber" placeholder="Enter Policy Number" th:readonly="${mode == 'UPDATE'}" />
</div>

<div class="form-group">
<label for="adjusterName">Adjuster</label>
<div class="input-group">
<input type="hidden" th:field="*{adjusterId.entityId}" class="form-control" id="adjusterId" />
<input type="text" th:field="*{adjusterId.entityName}" class="form-control" id="adjusterName" placeholder="Enter Adjuster" />
<div class="input-group-btn">
<button id="adjusterSearchBtn" type="button" class="btn btn-success"><span class="glyphicon glyphicon-search"></span></button>
<button id="adjusterCreateBtn" type="button" class="btn btn-success"><span class="glyphicon glyphicon-plus"></span></button>
</div>
</div>
</div>

</div>

<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
<a th:href="@{'/policy/list'}" class="btn btn-warning">Cancel</a>
</div>
</form>

问题出在我提交表单时; spring 表单绑定(bind)将表单参数绑定(bind)到 Policy 类的各个字段,但是如果未选择调整器,即 adjusterId 值的隐藏字段为空(这对于应用程序来说非常好)然后 spring 实例化一个新的实体类,所有字段都具有空值。由于 ManyToOne 关系和 JPA 验证,这会在应用程序的 JPA 持久性部分产生问题。

有关如何克服此问题的任何指示。如果 View (表单隐藏输入字段)中的 adjusterId 为空;那么表单绑定(bind)不应实例化具有空字段值的新实体类。相反,它应该将 Policy 类的 adjusterId 属性设置为 null。

顺便说一下,我已经回答过许多类似的问题,而这些问题中的大多数都不相关。其中之一就是 same question which is unanswered

提前致谢...

最佳答案

在 Controller 中,你可以这样做:

@RequestMapping(value = "/create", method = RequestMethod.POST)
public ModelAndView create(@ModelAttribute Policy p) {
if (p.getAdjusterId().getEntityId() == null) {
p.setAdjusterId(null);
}
policyService.create(p);
return new ModelAndView("viewName");
}

关于java - 嵌套对象空字段的 Spring 表单绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41309515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com