springboot20-打war包

1、编写controller

@Controller
public class UserController {

    @RequestMapping("/user/detail")
    public @ResponseBody Object userDetail(){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", 1001);
        map.put("username", "zhangsan");

        return map;
    }

}

2、添加依赖

        <!--Springboot内嵌tomcat解析jsp的依赖-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <!--在build标签中指定编译的位置-->
        <resources>
            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>*.*</include>
                </includes>
            </resource>
        </resources>

3、创建webapp目录

4、配置application.properties文件

spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

5、创建页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h3>用户姓名:${username}</h3>
<h3>用户年龄:${age}</h3>
</body>
</html>

6、编写controller进行环境测试

@RequestMapping("/user/page/detail")
    public String  detail(Model model){
        model.addAttribute("username", "王五");
        model.addAttribute("age", 20);

        return "userDetail";
    }

7、pom文件编写
(1)指定

(2)确认有无自带打包的插件

(3)指定打war包的名称

8、进行打包

打包成功后,在target目录中可以看到war包

9、修改启动类

@SpringBootApplication
public class Springboot17War01Application extends SpringBootServletInitializer {
    
    public static void main(String[] args) {
        SpringApplication.run(Springboot17War01Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }
}

10、将war包复制到webapp中,启动tomcat运行

posted @ 2021-09-15 16:41  不是孩子了  阅读(30)  评论(0编辑  收藏  举报