Witam,
Mam obiekt w którym jest kilka pól, potrzebuje tylko zaktualizować jedno z nich. Id zadania jest pobierane dobre. Metoda update to zwykłe merge z JPA. Niestety zamiast aktualizować obiekt, powstaje nowy z uzupełnionym tylko tym polem. Próbowałem z initBinderem i bez. Jak to zrobić poprawnie?
@RequestMapping(value = "/projects/other/project", method = RequestMethod.GET)
public String getStatusTaskForm(Map<String, Object> modelMap, @RequestParam("idProject") Long idProject, @RequestParam("idTask") Long idTask) {
String language = LocaleContextHolder.getLocale().getDisplayLanguage();
Map<String, String> statuses = commonService.statuses(language, false);
Task task = taskService.findById(idTask);
System.out.println("Id tego zadanka to: " + task.getId());
modelMap.put("statuses", statuses);
modelMap.put("statusOfTask", task);
Project project = projectService.findById(idProject);
User currentUser = userService.getUserFromContext();
if(project.getUsers().contains(currentUser) || currentUser.getEmail().equals(project.getEmailOfCreator())){
return "status";
}else{
return "errorAccess";
}
}
@RequestMapping(value = "/projects/other/project", method = RequestMethod.POST)
public ModelAndView processStatusTaskForm(HttpServletRequest request, @ModelAttribute("statusOfTask") Task
task, BindingResult result) {
String idProject = request.getParameter("idProject");
String idTask = request.getParameter("idTask");
String[] suppressedFields = result.getSuppressedFields();
if (suppressedFields.length > 0) {
throw new RuntimeException("Próba wiązania niedozwolonych pól: " + StringUtils.arrayToCommaDelimitedString(suppressedFields));
}
taskService.update(task);
return new ModelAndView("redirect:/projects/project?id="+ idProject);
// @InitBinder("statusOfTask")
// public void initialiseBinderInvention(WebDataBinder binder) {
// binder.setDisallowedFields("id", "name", "description", "dateStart", "dateEnd", "progress", "priority", "project");
// }
<form:form modelAttribute="statusOfTask" class="form-horizontal">
<div class="form-group">
<label for="status" class="col-sm-2 control-label">Status: </label>
<div class="col-sm-10">
<form:select path="status" class="form-control" id="sell">
<form:option value="" label="...."/>
<form:options items="${statuses}"/>
</form:select>
<form:errors path="status" cssClass="text-danger"/>
</div>
</div>
</div>
<div class="modal-footer">
<div class="form-group">
<div class="col-sm-12" id="btnSaveTask">
<input type="submit" value="<spring:message code="page.save"/>"
class="btn btn-md btn-primary pull-right"/>
</div>
</div>
</div>
</div>
</form:form>