1、SpringBoot项目使用Tomcat部署
1.1、修改项目启动类,自定义configure方法
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
//@EnableEurekaClient
@SpringBootApplication
@MapperScan("com.door.mapper")
public class DoorApplication extends SpringBootServletInitializer
{
public static void main( String[] args )
{
SpringApplication.run(DoorApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DoorApplication.class);
}
}
1.2、修改pom文件,去掉内置Tomcat,且不用指定版本
<!-- 部署到外部Tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
1.3、修改项目配置文件,给项目添加名称
server.servlet.context-path=/XXX
在此之前,访问某页面使用请求localhost:8080/index,现在需要加上项目名为localhost:8080/XXX/index
因为部署到Tomcat后也需要使用端口+项目名+请求访问
1.3.1、项目添加context-path后又衍生一个问题:之前页面中所有的请求都失效了,缺少项目名
如果使用thymeleaf,问题就好解决了
如果是资源文件,th:+href/src="@{资源文件地址}",该地址与使用内置Tomcat启动地址相同,只是包上@{}
<link rel="stylesheet" th:href="@{/reg_assets/css/sky-forms.css}"/>
<script th:src="@{/reg_assets/js/jquery-1.9.1.min.js}"></script>
如果是HTML中<a>标签,同上
<li ><a th:href="@{/XXX}">XXX</a></li>
困境解决 oye
2018-10-24更新:
thymeleaf中js获取项目名的方式:
var context_path = "[[@{/}]]";
这样获取到 /项目名/,所以ajax中url使用:
url : "[[@{/}]]face/search/url",