0

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.

2
  • 1
    Try with th:action="@{|/tasks/${task.id}|}" Commented Nov 9, 2017 at 9:00
  • Instead of reading from Path Varibale for a POST request, try to bind the projectId attribute in the task object itself and read it. Commented Nov 9, 2017 at 9:01

2 Answers 2

1

Update the action attribute in <form> with th:action="@{|/tasks/${task.id}|}"

I believe there is an id property with each task

Sign up to request clarification or add additional context in comments.

Comments

0

Do you iterate over each task within your html? You add a List of tasks (called findAllTasks) to the view so make sure you are using the correct object when posting the form. Otherwise there won't be an id.

You can put a already existing task (with an id) in your view and use that id when you post your form, or if you would like to create a new "task" with your post request just remove the id and wait for your repository to generate one when the object is created.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.