一、打成war包發佈到tomcat(這步已經完成,自行跳過)
1. pom.xml
去掉內嵌tomcat
`
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope><!-- 打成war的時候打開註釋,本地開發用內嵌tomcat時加上註釋 -->
</dependency>
`
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
打成war
`
<packaging>war</packaging>
`
- 1
- 2
- 3
- 4
2. 啓動類
`
@SpringBootApplication
@ComponentScan(basePackages = "com.yzker")
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
`
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
mvn clean package 將war包放到tomcat下試試吧,如果報404,參考下面
訪問地址:localhost:{tomcat端口號} /重命名的war包名/xxxx
二、404解決辦法
原因後續補充
1. pom.xml
`
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-legacy</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
`
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2. 修改web.xml
複製下面的代碼,修改com.yzker.test.application.Application爲你自己的啓動類
`
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.yzker.test.application.Application</param-value>
</context-param>
<listener>
<listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class>
</listener>
<!--
<filter>
<filter-name>metricFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>metricFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
`
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
3. 到此爲止,404的問題解決,如果還有問題,請評論,我們共同解決記錄
mvn clean package
將target目錄下生成war包放到tomcat裏吧
訪問地址:localhost:{tomcat端口號} /重命名的war包名/xxxx