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-小为为</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/

点击请求测试成功

 

猜你喜欢

转载自blog.csdn.net/yjt520557/article/details/88357489
今日推荐