1、启动类继承SpringBootServletInitializer并重写configure方法,代码如下:
package com.hr.jtgc;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
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;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.sql.DataSource;
@RestController
@SpringBootApplication
public class MyspringbootApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MyspringbootApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyspringbootApplication.class);
}
}
2、修改在pom.xml文件中中的packaging标签:
<groupId>com.it</groupId>
<artifactId>myspringboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- <packaging>jar</packaging>-->
<packaging>war</packaging>
3、清理原来的jar包,打成war包:
将target 目录中的 myspringboot-0.0.1-SNAPSHOT.war 文件复制到你所安装的tomcat目录中webapps目录中,然后去掉工程后边的后缀,变成 myspringboot.war,此时可以直接放到tomcat\webapps下,运行项目时为:http://localhost:8080/myspringboot/index.
4、配置tomcat虚拟路径
按照上面方式访问时和在springboot中访问多一个项目名称,当时在springboot中可以设置访问带项目名称,如果没有设置,此时在访问时有些文件的路径就会出问题,找不到问题,这时可以考虑采用配置tomcat虚拟路径的方式来解决。找到server.xml并在<host></host>中加入如下代码:
<Context path="/" docBase="D:\apache-tomcat-8.5.31\jtgc" reloadable="true" />
这时访问就不需要加项目名称了!直接http://localhost:8080/index 就可以了,和在springboot中访问方式一样