添加依赖
SpringBoot-starter-web内嵌的Tomcat无法解析jsp(Thymeleaf),需要额外添加类库:Jasper
1 2 3 4 5
| <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency>
|
添加上下文目录
中途会提示是否生成文件夹,确认下就行了,最后会多个webapp文件夹,效果如下↓
指定SpringBoot的启动目录
添加上$ContentRoot$
设置application.properties
1 2 3 4
| #页面默认前缀目录 默认在webapp下有别的文件夹可以,以文件夹/往下加 spring.mvc.view.prefix=/ #页面默认后缀目录 spring.mvc.view.suffix=.jsp
|
测试是否可行
==ViewController:==
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.keafmd.controller;
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping;
@Controller public class ViewController {
@GetMapping("getname") public String getName(Model model){ model.addAttribute("name","keafmd"); return "name"; } @GetMapping("getage") public String getAge(Model model){ model.addAttribute("age",18); return "age"; } }
|
在webapp下创建name.jsp和age.jsp
==name.jsp:==
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/4/2 Time: 14:31 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>name</h1> ${name}
</body> </html>
|
==age.jsp:==
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/4/2 Time: 15:29 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>age</h1> ${age}
</body> </html>
|
———-启动程序———-
访问:http://127.0.0.1:8080/getage
访问:http://127.0.0.1:8080/getname
搞定!
以上就是SpringBoot集成使用jsp(超详细)的全部内容。
加油!
共同努力!
Keafmd