I need to make post request with thymeleaf but have some problems My controller
@RequestMapping(value = "/tasks/{project}", method = RequestMethod.GET)
@ResponseBody
public ModelAndView index(@PathVariable Project project) {
ModelAndView modelAndView = new ModelAndView();
List<Task> findAll = taskService.findAll(project);
modelAndView.addObject("findAllTasks", findAll);
final String currentUser = SecurityContextHolder.getContext().getAuthentication().getName();
modelAndView.addObject("user", userService.findByEmail(currentUser));
modelAndView.setViewName("tasks");
return modelAndView;
}
@RequestMapping(value = "/tasks/{id}", method = RequestMethod.POST)
@ResponseBody
public ModelAndView createNewProject(@Valid Task task, BindingResult bindingResult, @PathVariable ("id") int id ) {
Project project = projectService.findOne(id);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("id", id);
if (bindingResult.hasErrors()) {
modelAndView.setViewName("tasks");
} else {
taskService.create(task, project);
modelAndView.addObject("successMessage", "Task has been added succesfully");
modelAndView.addObject("task", new Task());
modelAndView.setViewName("tasks");
List<Task> findAll = taskService.findAll(project);
modelAndView.addObject("findAllTasks", findAll);
}
return modelAndView;
}
my html
<form autocomplete="off" action="#" th:action="@{'/tasks/' + ${id}}"
th:object="${task}" method="post" class="form-horizontal"
role="form">
<input type="text" id="taskTitle" name="Title" th:placeholder="Title"
class="form-control" />
<input type="text" id="body" name="body" th:placeholder="text"
class="form-control" />
<input type="checkbox" id="public_feedback"
class="form-control" /><label for="public_feedback">Public feedback</label>
<span th:utext="${successMessage}"></span>
<button type="submit" class="add">Add task</button>
</form>
but my post request is http://localhost:8080/tasks/null. Get request working is good. and hardcoding for example like
th:action="@{/tasks/1}"
working well but i need take path for current project i need get project id by path variable by i can't find any answers. Help me please.
th:action="@{|/tasks/${task.id}|}"