Recently made a SpringBoot project, today introduced the SpringBoot JAR package and deployment to Tomcat, has a certain reference value, interested partners can refer to

The detailed steps

First, make some changes in the pom.xml file:

You need to change the way the war package is packaged before. This time, you don’t need to change the way the war package is packaged, because the default is the JAR package, specify the name of the final jar package, manually specify the path to compile the package in the Resources folder, and add SpringBoot embedded Tomcat dependencies for resolving JSPS (just for this instance).

<? 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0. 0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId>  <version>2.4. 5</version> <relativePath/> <! -- lookup parent from repository --> </parent> <groupId>com.songzihao.springboot</groupId> <artifactId>023-springboot-jar</artifactId>
    <version>1.0. 0</version>
 
    <properties>
        <java.version>11</java.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
    </dependencies>
 
    <build>
        <finalName>SpringBootJar</finalName>
 
        <resources>
            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>*.*</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*    
      
         org.springframework.boot
            1.4.2.RELEASE  
       Copy the code

And then I write a control layer, UserController

package com.songzihao.springboot.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import java.util.HashMap;
import java.util.Map;
 
/ * * * * /
@Controller
public class UserController {
 
    @RequestMapping(value = "/user/detail")
    public @ResponseBody Object userDetail(a) {
        Map<String,Object> map=new HashMap<>();
        map.put("id".1001);
        map.put("username"."Zhang Qiling");
        return map;
    }
 
    @RequestMapping(value = "/user/page/detail")
    public String userPageDetail(Model model) {
        model.addAttribute("id".1001);
        model.addAttribute("username"."Little brother");
        return "userDetail"; }}Copy the code

In the core configuration file, you configure the port number, context root, and view resolver embedded in Tomcat.

server.port=9090
server.servlet.context-path=/
 
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
Copy the code

Then write a corresponding JSP page in the control layer to do the simulation test.

<%@ page contentType="text/html; charset=utf-8" language="java"% > < HTML > < head > < title > $< / title > < / head > < body > < h3 > user number: ${id} < / h3 > < h3 > user name: ${username} < / h3 > < / body > < / HTML >Copy the code

Finally, there is the entry class for the SpringBoot project.

package com.songzihao.springboot;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class Application {
 
    public static void main(String[] args) { SpringApplication.run(Application.class, args); }}Copy the code

Start the test in IDEA

Once the project is packaged and deployed, start testing again

After the war package, we need to put the generated.war file into the Webapps directory of Tomcat.

This time it is a JAR package, and it generates a.jar file, which we can put in any directory.

Copy the springBootjar. jar to the specified directory, and in the current directory, enter CMD to the command line window.

Then enter the command: Java -jar springbootjar.jar to start!!

Changes to Tomcat’s port number and context root after the SpringBoot project jars

When we deployed the project after Tomcat, the Tomcat port number and context root in our core configuration file were invalid, and the local Tomcat should prevail.

This time it’s a JAR package, because we’re not deploying to the native Tomcat, so we’re still using the built-in Tomcat provided by the SpringBoot framework. The port number and context root declared in the core configuration file application.properties should be used.