SpringBoot集成使用jsp
添加依赖
SpringBoot-starter-web内嵌的Tomcat无法解析jsp(Thymeleaf),需要额外添加类库:Jasper
<!-- 解析jsp类库 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
添加上下文目录
中途会提示是否生成文件夹,确认下就行了,最后会多个webapp文件夹,效果如下↓
指定SpringBoot的启动目录
添加上$ContentRoot$
设置application.properties
#页面默认前缀目录 默认在webapp下有别的文件夹可以,以文件夹/往下加
spring.mvc.view.prefix=/
#页面默认后缀目录
spring.mvc.view.suffix=.jsp
测试是否可行
ViewController:
package com.keafmd.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
/**
* Keafmd
*
* @ClassName: ViewController
* @Description:
* @author: 牛哄哄的柯南
* @Date: 2021-04-02 14:47
* @Blog: https://keafmd.blog.csdn.net/
*/
@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:
<%--
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:
<%--
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