我有一个对象,里面有一个嵌套的复杂对象:
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">中,就像保存id一样。
主计长:
@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。因此,我猜对象没有正确地存储在隐藏字段中。如何正确传递对象,使其在创建或编辑过程中不会丢失?
发布于 2016-10-29 19:29:03
我现在以不同的方式解决了我的问题。我从这个隐藏的字段移开,并将我的对象存储在一个会话中。在保存方法中,将它们从会话中提取出来,并将其再次添加到我的监控系统对象中。它工作得很好,没有丢失任何数据。
此外,它更节省,因为隐藏字段可以很容易地被用户操作。
但是谢谢你的帮助。
发布于 2016-10-29 18:12:34
你把整个物体都绑定到这个领域了。它应该是
<input type="hidden" th:field="*{anonymization.id}"/>https://stackoverflow.com/questions/40321558
复制相似问题