Issue
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?
Solution
I solved my problem in a different way now. I moved away from this hidden fields and store my objects in a Session. In the saving-method is pull them out of the session and add it in my monitoringSystem-object again. This works perfectly and no data ist lost.
Futhermore it is more save because hidden fields can be easiely manipulated by the user.
But thanks for your help.
Answered By - Daniel
Answer Checked By - Katrina (JavaFixing Volunteer)