0

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

Mahozad
  • 18,032
  • 13
  • 118
  • 133
Urban
  • 585
  • 2
  • 13
  • 28

2 Answers2

0

instead of @ModelAttribute you have to use @RequestParam

@RequestMapping(value = "/jiraImport", method = RequestMethod.POST)
 public String import(@RequestParam(name = "project") Integer projectID){
//System.out.println(projectID);

   return "redirect:/projects";
}
Mark
  • 11
  • 2
  • This wont work because it is not a request parameter – akuma8 Oct 06 '17 at 08:09
  • There was an unexpected error (type=Bad Request, status=400). Required Integer parameter 'project' is not present is the error I get when using your solution – Urban Oct 06 '17 at 08:09
0

I'd never used thymeleaf but as you pointed out, you should add a model attribute if you want to use @ModelAttribute.

I sugget you to proceed like this :

<select class="form-control">
       //each <option> tag should be a "/jiraImport"  parameterized by ${project.id} like this :
    <option> <a href="/jiraImport?projectID=${project.id}"...>...
</select>

The idea is here, I let you use thymeleaf to do it correctly.

In the back you can do this :

@RequestMapping(value = "/jiraImport")
public String import(@RequestParam("projectID") Integer projectID){
  //System.out.println(projectID);
  return "redirect:/projects";
}

2nd solution : Add an empty project object in the model

@RequestMapping(value = "/Import", method = RequestMethod.GET)
public String import(Model model){
    List<Project> projects = this.projectService.getAllProjects();
    model.addAttribute("projects", projects);
    model.addAttribute("project", new Project());
    return "Import";
}

Then in the view :

<form method="POST" th:object="${project}">
   <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>
</form>

Finally in the back :

@RequestMapping(value = "/jiraImport", method = RequestMethod.POST)
public String import(@ModelAttribute("project") Project project){
  System.out.println(project.getId);

  return "redirect:/projects";
}

As said, I don't use Thymeleaf so I let you complete this code, e.g adding the submit button and the url to post the form.

Let us know

akuma8
  • 4,160
  • 5
  • 46
  • 82
  • Thanks for the effort. I tried it but in my controller still getting null as the projectID. When i inspect the html i clearly see that the projectID's are not null but all Integers. – Urban Oct 06 '17 at 08:35
  • Did you add "new Project()" in the model and set "project.id" in the form, using th:value? – akuma8 Oct 06 '17 at 08:42
  • I did that and i'm getting null as the ID in my controller – Urban Oct 06 '17 at 08:44
  • What about the first solution? Parameterized url? – akuma8 Oct 06 '17 at 08:47
  • I tried the second one and not the first one. My form contains the dropdown with the project objects and another dropdown containing strings. If i press the submit button i want to get bot values. I am getting the string from the other dropdown. Using a link in my dropdown isn't very nice either i think – Urban Oct 06 '17 at 08:51
  • You're right, did you add the "th:field" attribute in the "select" tag ? – akuma8 Oct 06 '17 at 09:16