1:在pom.xml中把jar改为war
<groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>demo</name> <description>Demo project for Spring Boot</description>
2.pom.xml并且添加下面一段代码
<!--因配置外部TOMCAT 而配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
3:找到<build></build>中,添加下方红色代码。打war包后war包的名字
<build> <finalName>demo02</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
此时项目可能会有个红x,按照下图。右键工程选择maven,再选择update project...,弹窗中选择本项目,最下方点击ok红叉即消失
4:重要一步:Demo02Application启动类改为,否则打包后的war包在tomcat中无法启动。
package com.my.sb; 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; @SpringBootApplication public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { // TODO Auto-generated method stub return builder.sources(Application.class); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
5:打war包,右键工程,run as 后选择maven install 等待编译
6:编译好后,打开项目target中可以看到所打包的demo02.war包。复制。放到tomcat中既可
tomcat访问还是老规矩需要加上项目名字:
http://localhost:8080/sb/user/test?inputName=qwe
tomcat访问还是老规矩需要加上项目名字:
http://localhost:8080/sb/user/test?inputName=qwe
tomcat访问还是老规矩需要加上项目名字:
http://localhost:8080/sb/user/test?inputName=qwe
tomcat访问还是老规矩需要加上项目名字:
http://localhost:8080/sb/user/test?inputName=qwe