I have an Object with a nesten complex Object inside:
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;
When i create a new object or edit an exsiting one, i want to keep my anonymization object. To do so i tried to save it in an <input type="hidden"> like i do to keep my id.
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:
<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>
Unfortunatly this dosen't work. When I try to get the anonymization-object in the save-method with monitoringSystem.getAnonymization() I'm getting a Nullpointerexception. So I guess the object isn't stored in the hidden field correctly. How can I pass the object correctly so that it isn't lost during the create or edit process?
You're binding whole object to the field. It should be
<input type="hidden" th:field="*{anonymization.id}"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With