SpringBoot项目打成war包使用

1.我们使用idea的Spring initializr

创建一个项目我们在这里注意将默认的jar替换为war

然后我们选择web模块

 

 

 

2.项目创建成功之后我们修改pom文件排除内部tomcat

我们将原来的web pom文件修改为如下

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

 3.我们建立webapp文件夹和web.xml文件

 

然后项目结构如下 这里的这两个jsp文件是我后来创建的

4.   写页面和控制器

控制器代码 

 

package com.example.demo.controller;

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

/**
 * @ClassName HelloController
 * @Author ywj
 * @Describe
 * @Date 2019/3/8 0008 19:20
 */
@Controller
public class HelloController {


    @GetMapping("/hw")
    public String hello(Model model){
        model.addAttribute("msg","哎呦不错哦");
        return "success";
    }
}

 

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/3/8 0008
  Time: 19:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"  isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>hello-spring</h1>
<a href="${pageContext.request.contextPath}/hw">请求</a>
</body>
</html>

success.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/3/8 0008
  Time: 19:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${msg}
</body>
</html>

application.properties

spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp

5.生成war包

点击这个即可

出现如下代表生成成功,箭头所指的是war包地址 

 

6.然后我们将war包放置到tomcat的webapps中

 

然后启动tomcat

启动成功

然后我们在浏览器访问http://localhost:8080/加上war包名

http://localhost:8080/demo-0.0.1-SNAPSHOT/

 

点击请求测试成功

 

7.将原来的项目打包为war包项目

第一步修改主启动类:

     

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DemoApplication.class);
    }

}

第二步:修改pom文件

接下来的和之前一样打包即可也可以

若想修改打war包的名字,那么加如下配置

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <finalName>springboot</finalName>
    </configuration>
</plugin>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值