Home > Mobile >  Failed to convert value of type 'java.lang.String' to required type 'java.lang.Intege
Failed to convert value of type 'java.lang.String' to required type 'java.lang.Intege

Time:09-06

After I added pagination to my web application, now I looking to filter the data by adding a search bar ( by name ).

This is the error :

Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer';
 nested exception is java.lang.NumberFormatException: For input string: "{pageNumber}" 
 org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to 
 convert value of type 'java.lang.String' to required type 'java.lang.Integer'; 
 nested exception is java.lang.NumberFormatException: For input string: "{pageNumber}"

This is the method in my controller :

@GetMapping(value = "/medecin/{pageNumber}")
    public String list(@PathVariable Integer pageNumber, Model model,
                       @RequestParam(name = "size", defaultValue = "4") int size,
                       @RequestParam(name = "keyWord", defaultValue = "") String keyWord) {

        Page<Medecin> page ;

        if (keyWord==null)
             page = medecinRepository.findAll(PageRequest.of(pageNumber - 1, size));
        else
             page = medecinRepository.findByNomContains(keyWord, PageRequest.of(pageNumber - 1, size));

        List<Medecin> medecinList = page.getContent();

        long totalItems = page.getTotalElements();

        int totalPages =  page.getTotalPages();


        model.addAttribute("list", medecinList);
        model.addAttribute("totalItems", totalItems);
        model.addAttribute("totalPages", totalPages);
        model.addAttribute("pageNumber", pageNumber);

        return "medecin/list";
    }

and this is the part of my HTML file:

<form th:action="@{/medecin/{pageNumber}}">
        <button type="submit" >CHERCHER</button>
        <input type="text" name="keyWord" th:value="${keyWord}">
</form>

I want to notice that when replace <form th:action="@{/medecin/{pageNumber}}"> by <form th:action="@{/medecin/1}"> the application shows the correct results.

I had to fix it considering that after searching the result can be more than one page.

CodePudding user response:

I think the problem is how you construct your th:action attribute. The way your are doing it, you are passing the literal {pageNumber} instead of it's resolved value.

The correct way would probably be:

<form th:action="@{/medecin/{path}(path=${pageNumber})}">

See this thread for more explanation: Thymeleaf construct URL with variable

CodePudding user response:

Try this. add @PathVariable("pageNumber")

    public String list(@PathVariable("pageNumber") Integer pageNumber, Model model,

If this doesn't work, trying making pageNumber int instead of Integer.

CodePudding user response:

You have two options to do it

First assign variable with value pageNumber to the path variable named pageNumber

<form th:action="@{/medecin/{pageNumber}(pageNumber = ${pageNumber})}">
        <button type="submit" >CHERCHER</button>
        <input type="text" name="keyWord" th:value="${keyWord}">
</form>

Second one, you can create a string with single quote inside double quote to produce a path with parameter named pageNumber

<form th:action="@{'/medecin/'   ${pageNumber}}">
        <button type="submit" >CHERCHER</button>
        <input type="text" name="keyWord" th:value="${keyWord}">
</form>
  • Related