本地部署到服务器上的资源路径问题
创始人
2025-01-18 20:33:47
0

本地部署到服务器上的资源路径问题

服务器端的源代码的静态资源目录层级

在这里插入图片描述

当使用Thymeleaf时,在templates的目录下为返回的html页面,下面以两个例子解释当将代码部署到tomcat时访问资源的路径配置问题

例子一 index.html(在templates的根目录下)

调用controller的接口

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> results = new ArrayList<>();//最终要存放的数据         List discussPosts = discussPostService.findDiscussPosts(0, page.getOffset(), page.getLimit());//由用户给出的参数,查询帖子         //遍历帖子,将帖子的信息和用户的信息封装到Map中         for (DiscussPost post : discussPosts) {             User user = userService.findUserById(post.getUserId());//根据帖子的userId查询用户             Map map = Map.of("post",post,"user",user);//将帖子和用户封装到Map中             results.add(map);//将Map添加到List中         }         model.addAttribute("results",results);//将List添加到model中         return "/index";//返回模板的路径,在resources/templates下的index.html     } } 
点击F12在浏览器查看部署到Tomcat的资源路径

在这里插入图片描述

由上图可以看出index的页面在http://localhost:8080/community的目录下,而css与js都分别在http://localhost:8080/community/css/和http://localhost:8080/community/js/下,因此静态资源的路径都会在根目录即http://localhost:8080/community下

那么如果我们要在index.html中使用js和css文件,我们的访问路径就应该是http://localhost:8080/community/css/bootstrap.min.css和http://localhost:8080/community/js/global.js

而下面使用@配置的恰恰是相对路径

  

当使用/开头时也就是根目录,而当前项目的根目录为http://localhost:8080/community 其中的community是我们在application.yml中所配置的项目名称

server:   port: 8080   servlet:     context-path: /community 

也就是说静态资源目录(src/main/resources/static)下的资源都会被tomcat注入到根目录下http://localhost:8080/community

例子二

http://localhost:8080/community/discuss/detail/274

对于这个请求的tomcat目录如下图

在这里插入图片描述

此时如果想要在html中访问到静态资源,目录还依然应该是http://localhost:8080/community/css/和http://localhost:8080/community/js/

但是此时的页面路径为http://localhost:8080/community/discuss/detail/274,那么想要达到根目录可以使用/也可以使用…/来退到上级

如果使用/代码仍然为

  

但是也可以使用…/,但是不具有普适性,需要根据路径来自定义…/的数量

  

但是如果此时代码为那么html依然无法解析到tomcat中的静态资源,因为此时实际路径仍然为http://localhost:8080/community/js/,但是由于我们使用了一个…/,那么此时的访问路径为http://localhost:8080/community/discuss/js/,仍然会存在问题

相关内容

热门资讯

专业透视!pokemmo内置修... 专业透视!pokemmo内置修改器,aapoker怎么提高中牌率,操作教程(有挂详情)-哔哩哔哩1、...
科普!全民比鸡手游辅助,aap... 科普!全民比鸡手游辅助,aapoker怎么开辅助器,总结教程(有挂实锤)-哔哩哔哩1、下载好全民比鸡...
教你透视!wepoker有没有... 教你透视!wepoker有没有挂,hhpoker辅助软件是真的么,教材教程(有挂方式)-哔哩哔哩所有...
记者发布!指尖四川辅助破解版苹... 记者发布!指尖四川辅助破解版苹果,wepoker软件靠谱么,资料教程(存在有挂)-哔哩哔哩1、完成指...
了解透视!wepoker透视脚... 了解透视!wepoker透视脚本视频,德州来玩辅助器,课程教程(有挂秘籍)-哔哩哔哩1、上手简单,内...
避坑细节!泸州大二新手攻略,w... 避坑细节!泸州大二新手攻略,wepoker-h5下载,要领教程(有挂神器)-哔哩哔哩1、许多玩家不知...
辅助透视!hhpoker辅助靠... 辅助透视!hhpoker辅助靠谱吗,hhpoker免费辅助器,课程教程(有挂攻略)-哔哩哔哩1、任何...
盘点一款!!福建天天开心辅助器... 盘点一款!!福建天天开心辅助器真的假的,wepoker手机插件,绝活教程(有挂技巧)-哔哩哔哩1、金...
曝光透视!wepoker私人局... 曝光透视!wepoker私人局怎么玩,cloudpoker作必弊,课程教程(有挂教程)-哔哩哔哩1、...
六分钟了解!hhpoker作笔... 六分钟了解!hhpoker作笔实战视频,竞技联盟辅助插件,教程书教程(有挂猫腻)-哔哩哔哩1、该软件...