Spring Boot 配置项目路径

背景

这两天做Spring Boot项目,打完jar包后发给业务方,项目只能作为子模块嵌入在对方的系统里,请求时需要加入项目路径。

Spring Boot配置项目路径

  • Spring Boot 2.0之前,配置为 server.context-path
  • Spring Boot 2.0之后,配置为 server.servlet.context-path
  • 默认值server.servlet.context-path=/ ,访问url为 127.0.0.1:8080/index
  • 配置 server.servlet.context-path=/hello,访问url为 127.0.0.1:8080/hello/index

Thymeleaf 前端页面url添加项目路径

1、thymeleaf 中 th:href 和 href 的区别

语法格式如下:

<a th:href="@{/app/lise}">应用列表</a>
<a href="/app/list">应用列表</a>
  1. 默认项目路径为空时,打Jar包单独运行时:二者效果一致
  2. 在使用Maven内嵌Tomcat或打War包部署到Servlet容器,或者在项目内执行App启动类,且有配置项目路径
  • href始终从端口开始作为根路径,如 http://127.0.0.1:8080/app/list
  • th:href会寻找项目路径作为根路径,如 http://127.0.0.1:8080/hello/app/list
2、引入CSS、JS、Img文件
<link rel="stylesheet" type="text/css" th:href="@{/css/common.css">
<script type="text/javascript" th:src="@{/js/jquery/jquery.js}"></script>
<img th:src="@{/img/logo.png}">

3、JS内获取项目路径contextPath的三种方式

<script th:inline="javascript" type="text/javascript">
    var contextPath  = [[@{/}]];
    var contextPath = /*[[@{/}]]*/'';
    var contextPath=[[${#httpServletRequest.getContextPath()}]];
</script>

4、图标ico配置

  • 默认 favicon.ico 文件在 resources 目录下
  • 添加了项目路径后,图标 ico 访问也无法展示了,因此需要在头文件里添加下面一段
<link rel="shortcut icon" th:href="@{/favicon.ico}"/>
发布了18 篇原创文章 · 获赞 0 · 访问量 501

猜你喜欢

转载自blog.csdn.net/qingqingxiangyang/article/details/104426413