gpt4 book ai didi

spring - thymeleaf 和 spring-boot 以形式丢失的复杂对象

转载 作者:行者123 更新时间:2023-12-05 05:22:32 26 4
gpt4 key购买 nike

我有一个对象,里面有一个嵌套的复杂对象:

public class MonitoringSystem {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
private String url;
private String username;
private String password;
@Transient
private String passwordConfirm;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name ="anonymization_id")
private Anonymization anonymization;

当我创建一个新对象或编辑一个现有对象时,我想保留我的匿名化对象。为此,我尝试将其保存在 <input type="hidden"> 中。就像我保留我的身份证一样。

Controller :

@Controller
public class MonitoringSystemController {

// some Code

@RequestMapping("monitoringsystem/edit/{id}")
public String edit(@PathVariable Long id, Model model) {
model.addAttribute("monitoringsystem", monitoringSystemRepository.findOne(id));
return "monitoringsystem/form";
}

@RequestMapping("/monitoringsystem/new")
public String newMonitoringSystem(Model model) {
model.addAttribute("monitoringsystem", new MonitoringSystem());

return "monitoringsystem/form";
}

@RequestMapping(value = "/monitoringsystem/save", method = RequestMethod.POST)
public String save(MonitoringSystem monitoringSystem) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User user = userRepository.findByUsername(auth.getName());
long id = user.getCloudProvider().getId();

anonymizationRepository.save(monitoringSystem.getAnonymization());

// more code
}
}

表格:

<form class="form-horizontal" th:modelAttribute="monitoringsystem"
th:object="${monitoringsystem}" th:action="@{/monitoringsystem/save}" method="post">
<input type="hidden" th:field="*{id}"/>
<input type="hidden" th:field="*{anonymization}"/>
<fieldset>
<legend>New Monitoring-System</legend>
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Systemname</label>
<div class="col-md-5">
<input th:field="*{name}" class="form-control input-md" type="text"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">URL</label>
<div class="col-md-5">
<input th:field="*{url}" class="form-control input-md" type="text"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Username</label>
<div class="col-md-5">
<input th:field="*{username}" class="form-control input-md" type="text"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Password</label>
<div class="col-md-5">
<input th:field="*{password}" class="form-control input-md" type="password"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Confirm Password</label>
<div class="col-md-5">
<input th:field="*{passwordConfirm}" class="form-control input-md" type="password"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="singlebutton"></label>
<div class="col-md-4">
<a th:href="@{/monitoringsystem}" class="btn btn-default btn-small">Cancel</a>
<button id="singlebutton" name="singlebutton" class="btn btn-primary btn-small">
Submit
</button>
</div>
</div>
</fieldset>
</form>

不幸的是,这不起作用。当我尝试使用 monitoringSystem.getAnonymization() 在保存方法中获取匿名对象时我得到一个 Nullpointerexception .所以我猜对象没有正确存储在隐藏字段中。如何正确传递对象,使其在创建或编辑过程中不丢失?

最佳答案

您将整个对象绑定(bind)到该字段。应该是

<input type="hidden" th:field="*{anonymization.id}"/>

关于spring - thymeleaf 和 spring-boot 以形式丢失的复杂对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40321558/

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