Search code examples
htmlspring-bootthymeleaf

Failed to convert value of type 'java.lang.String' to required type 'long';


I'm building a course booking website. Now working on an "edit" function. As admin i can edit the existing page and sava it. I tried to write it but had following error:

 Failed to convert value of type 'java.lang.String' to required type 'long'; nested exception is java.lang.NumberFormatException: For input string: "editKursInfo"]

My controller:

     @GetMapping("/editKursInfo/{id}")
     public String editKursInfo(Model model, @PathVariable("id") long id,
            HttpSession session) {
        Kurs kurs = data.getKursById(id);
        model.addAttribute("kurs", kurs);
        model.addAttribute("edit", kurs);
        return "editKursInfo";
    }

    @PostMapping("/editKursInfo/{id}")
    public String editKursInfo(Model model,
            @Valid @ModelAttribute("edit") Kurs kurs,
            BindingResult bindingresult ,@PathVariable("id") long id
    ) {
      
        data.saveKurs(kurs);
        return "redirect:/editKurs";
    }

And Html:

 <form action="editKursInfo" method="POST"
                  th:object="${edit}">
                <table width="100%" align="center">
                    <tr>
                        <td>
                            <table  width="100%">  
                                <tr>
                                <tr>   <td align="left" >Kurs Name</td>
                                    <td>
                                        <input th:field="*{kursName}"                                   
                                               type="text"
                                               align="left"                                
                                               class="form-control"
                                               style="width:250px;"                                     
                                               th:errorclass="is-invalid"
                                               id="kursNameInput"
                                               value=""/><br>
                                        <div th:if="${#fields.hasErrors('kursName')}" th:errors="*{kursName}" class="invalid-feedback">          </div>
                                    </td> 
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>                
                <button type="submit" id="submit" class="btn btn-primary" style="float: right;margin-right: 85px; width: 150px;" >submit</button>                
            </form>

Solution

  • The action attribute of the <form> tag does not have the id path variable that your @PostMapping requires.

    Adjust to use something like:

    <form th:action="@{/editKursInfo/{id}(id=${edit.id})}" 
          method="POST" 
          th:object="${edit}">
    

    PS: In your @GetMapping method, you have the same Java object under 2 keys:

    model.addAttribute("kurs", kurs);
    model.addAttribute("edit", kurs);
    

    There is no need for that.

    PS 2: It is better not to use edit in your URL. I would suggest to use /kurse/{id} or /kurseinfo/{id}. The fact that you are editing something or just viewing something is determined by the GET or POST mapping.