栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

idea部署springboot项目到外部tomcat

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

idea部署springboot项目到外部tomcat

【README】

本文旨在记录idea部署springboot项目到外部tomcat的步骤;

第一次部署会踩很多坑儿,多查google,多重试;

第一次部署,不建议手动录入依赖,因为有可能遗漏;而且网络上资料很多但也很乱,很容易出错和掉坑儿里;


【1】创建项目 


【2】 引入 spring web相关依赖 1)选择依赖

 点击finish

2)项目pom


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    
    com.cmc
    springbt-04-web-jsp2
    0.0.1-SNAPSHOT
    war
    springbt-04-web-jsp2
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


使用外部tomcat,而不是内嵌tomcat的方式有两种, 官方文档推荐的就是上面这一种 provided;

官方文档参见  “How-to” Guides

3)整体目录结构


【3】创建web.xml,设置根路径 1)进入 项目结构页面的module页面 (快捷键 ctrl+alt+shift+s)

2) 创建web根目录 srcmainwebapp

 双击创建;

3) 创建web.xml文件

根目录(D:workbench_ideaspring-bucketspringbootspringbt-04-web-jsp2srcmainwebapp)下WEB-INF下创建web.xml 文件

 注意: 路径不要写错了 ;

4)创建完成后的效果


  【4】配置外部tomcat(这个简单) 1)进入配置页面

2)添加tomcat


【5】部署springboot项目到外部tomcat 1)进入部署页面

2)选择 exploded web制品库


【6】编写web页面和controller 1)编写web页面

hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    hello


    hello world.

2)启动tomcat,查看访问效果

3)添加 controller 3.1)MyController
package com.cmc.springbt.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MyController {
    @GetMapping("/aaa")
    public String hello(Model model) {
        System.out.println("hello aaa");
        model.addAttribute("msg", "hello aaa");
        return "success";
    }
}
3.2)修改hello.jsp 

    hello world.
    aaa
3.3)添加 WEB-INFsuccess.jsp 
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    success


    SUCCESS
    ${msg}

添加视图前后缀
application.properties 
# 设置视图路径前后缀
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
# 设置应用访问根路径
server.servlet.context-path=/springbt_04_web_jsp2
3.4)目录结构 

3.5)访问效果

4)springboot启动主程序

Springbt04WebJsp2Application

public class Springbt04WebJsp2Application {

    public static void main(String[] args) {
        SpringApplication.run(Springbt04WebJsp2Application.class, args);
    }

}
ServletInitializer
public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Springbt04WebJsp2Application.class);
    }
}


【7】小结

step1)必须创建一个 war项目;

step2)将嵌入式的tomcat设置为provided,表示 不打入 war包;


            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        

step3)必须编写一个  SpringBootServletInitializer 实现类; 目的调用 configure方法;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        // 传入springboot应用主程序
        return application.sources(Springbt04WebJsp2Application.class);
    }
}

 

step4)启动tomcat即可

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/322169.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号