Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

java - How to add css files to Spring boot application (Thymeleaf) pagination

I try to make pagination in my web site.

my PostController.java file contains:


@GetMapping("/")
    public String index(Model model) {
        return findPaginated(0, model);
    }


@GetMapping("/page/{pageNo}")
    public String findPaginated(
            @PathVariable("pageNo") int pageNo,
            Model model
    ) {
        int pageSize = 3;

        Page<Post> page = postService.findPaginated(pageNo, pageSize);
        List<Post> listPosts = page.getContent();

        model.addAttribute("currentPage", pageNo);
        model.addAttribute("totalPages", page.getTotalPages());
        model.addAttribute("totalItems", page.getTotalElements());

        model.addAttribute("page", page);

        int totalPages = page.getTotalPages();

        if (totalPages > 0) {
            List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages).boxed().collect(Collectors.toList());
            model.addAttribute("pageNumbers", pageNumbers);
        }

        model.addAttribute("listPosts", listPosts);

         return "index";
    }

and index.html file contains:


<head xmlna:th="http://www.thymeleaf.org">
--snip--
<link href="assets/css/font-awesome.min.css" rel="stylesheet">
--snip--

</head>

<body>

--snip--

 <!-- ADDING PAGINATION-->

                <nav aria-label="...">
                    <ul class="pagination">
                        <li class="page-item">
                            <a class="page-link"
                               th:href="@{'/page/' + ${currentPage - 1}}"
                               th:if="${currentPage < totalPages + 1 and currentPage > 1}">Previous</a>
                        </li>
                        <li th:class="${pageNumber == page.getNumber() + 1} ? 'page-item active': 'page-item'"
                            th:each="pageNumber: ${pageNumbers}" th:if="${page.getTotalPages() > 0}">
                            <a class="page-link"
                               th:href="@{'/page/' + ${pageNumber}}"
                               th:text="${pageNumber}">
                            </a>
                        </li>
                        <li class="page-item">
                            <a class="page-link"
                               th:href="@{'/page/' + ${currentPage + 1}}"
                               th:if="${currentPage < totalPages and currentPage >= 1}">Next</a>
                        </li>
                    </ul>
                </nav>

                <!-- /ADDING PAGINATION-->

--snip--

</body>

When program runs for the first time everything looks right. So I add some posts (this app is about writing posts and showing them in index.html file) and when I want to go for example: localhost:8080/page/1 css, js and img files didn't upload. I inspected and checked network, and it shows me localhost:8080/page/assets/css/font-awesome.min.css didn't upload.Actually, it must be localhost:8080/assets/css/font-awesome.min.css . But why exactly this path it shows me?

How could I fix it?

If you didn't get question, then I uploaded some extra images for better understanding:

Here is folders containing html and css

This is my html file with thymeleaf

When program starts and add some posts we can see this part

If I click for example 2 then css, js and img files didn't upload

here is the link

This is what we can see when inspecting localhost:8080/page/2

Thanks in advance. I hope you get it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try to add additional / at start here before assets

<link href="/assets/css/font-awesome.min.css" rel="stylesheet">

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...