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>