Spring Boot自带Tomcat插件,但是在实际开发中,服务器一般是已经建好了的,由专人维护,因此要去除Spring Boot自带的Tomcat,再将Spring Boot项目打包成war发布到外部的服务器如Tomcat
开发环境
IntelliJ IDEA 2018、JDK 1.8、Tomcat 8.5.43
项目结构
步骤
1.配置pom.xml文件,注意加注释的地方
需要去除Tomcat插件、配置Tomcat版本、打包的war名称、添加项目依赖…
// pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>menu</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging><!-- 打war包 -->
<name>menu</name>
<description>menu project for Spring Boot</description>
<!-- 继承父包 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/>
</parent>
<!-- 配置java和Tomcat,Tomcat需8.5以上版本,请根据自己的Tomcat版本配置 -->
<properties>
<java.version>1.8</java.version>
<tomcat.version>8.5.43</tomcat.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope><!-- 去除内置tomcat -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<!-- ...你项目所需的依赖 -->
</dependencies>
<build>
<finalName>MavenSpringBoot</finalName>
<plugins>
<!-- war部署使用 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>menu</warName><!-- 设置打包后war包的名字 -->
</configuration>
</plugin>
</plugins>
</build>
</project>
2.编写启动类
程序入口,需要继承SpringBootServletInitializer、重写configure方法(启动类只能有一个,不能乱放请参照上面的项目结构图)
package com.menu;//改为自己项目的地址
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;
/**
* SpringBoot启动类
*/
@SpringBootApplication
public class MenuApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MenuApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(MenuApplication.class);
}
}
3.编写测试方法
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
/**
* test
*/
@RequestMapping("/test")
public String test(Model model){
model.addAttribute("username","一方通行");
return "test";
}
}
4.编写测试页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
hello !
<p th:text="${username}"></p>
</body>
</html>
5.打war包
1.打开idea右侧栏Maven 双击 Maven clean(清除缓存)
2.双击 Maven install(打包成war)
3.查看target文件夹下找到打包好的war文件
4.将该war包放入Tomcat --> webapps文件夹下,运行Tomcat(bin 目录中 startup.bat双击),Tomcat自动解压war文件后
打开浏览器输入http://127.0.0.1:8080/menu/test
tomcat相关问题
1.配置环境变量 JAVA_HOME
2.tomcat控制台打印日志乱码问题
在tomcat-8.5.43\conf\logging.properties中
java.util.logging.ConsoleHandler.encoding = GBK //或UTF8,我的是加GBK
3.去包名
在tomcat-8.5.43\conf\server.xml中host标签中加
《Context path="/" docBase=“menu” reloadable=“true”/》
然后启动打开浏览器输入http://127.0.0.1:8080/test 就能打开网页了
4.startup.bat启动闪退
打开startup.bat在末尾:end后换行 加上pause