I have this piece of code in my controller to get to a page with a field with a form.
@RequestMapping(value = "/Import", method = RequestMethod.GET)
public String import(Model model){
List<Project> projects = this.projectService.getAllProjects();
model.addAttribute("projects", projects);
model.addAttribute("type", "");
return "Import";
}
This is the part of thymeleaf view which is used
<div class="form-group" th:object="${project}">
<label class="col-md-4 control-label">Project</label>
<select class="form-control">
<option th:each="project : ${projects}" th:value="${project.id}" th:text="${project.name}"></option>
</select>
</div>
I want to retrieve the selected value when the user clicks the submit button and this is the controller method that is supposed to handle it:
@RequestMapping(value = "/jiraImport", method = RequestMethod.POST)
public String import(@ModelAttribute("project") Integer projectID){
//System.out.println(projectID);
return "redirect:/projects";
}
I get the error:
java.lang.NoSuchMethodException: java.lang.Integer.<init>()
I think because there is no such ModelAttribute called Project. How can i get the selected id of the project in my controller?
EDIT
@RequestMapping(value = "/jiraImport", method = RequestMethod.POST)
public String import(@ModelAttribute("project") Project project){
System.out.println(project.getID());
return "redirect:/projects";
}
Changed it to this. But now the ID of the project is null. But i can see the th:value being the actual ID of the project