简体   繁体   中英

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

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" class="btn btn-primary">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.

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

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.

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" class="btn btn-primary">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" class="btn btn-primary">CHERCHER</button>
        <input type="text" name="keyWord" th:value="${keyWord}">
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM