Technology Sharing

Resource path problem when deploying locally to the server

2024-07-11

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Resource path problem when deploying locally to the server

Server-side source code static resource directory hierarchy

insert image description here

When using Thymeleaf, the returned HTML page is in the templates directory. The following two examples explain the path configuration problem of accessing resources when deploying code to Tomcat

Example 1 index.html (in the root directory of templates)

Call the controller interface

http://localhost:8080/community/index

    @GetMapping("/index")
    public String getIndexPage(Model model, Page page){
        //方法调用前,SpringMVC会自动实例化Model和Page,并将Page注入Model,
        // 所以,在thymeleaf中可以直接访问Page对象中的数据,不需要使用model.addAttribute()方法进行数据模型的添加
        page.setRows(discussPostService.findDiscussPostRows(0));//查询帖子的总数
        page.setPath("/index");//设置当前的访问路径,用于前端复用分页链接
        List