1. 引入JSP的依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
2. 构建JSP目录
JSP目录3. 配置JSP和数据库
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
4. 让Springboot项目能创建JSP文件
-
打开Project Structure,进行如下配置
打开Project Structure,进行如下配置
5. 创建一个JSP的文件
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/4/11
Time: 15:29
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>这是springboot下的jsp</title>
</head>
<body>
welcome!!!<%=request.getAttribute("name")%><br>
你好,${name}
</body>
</html>
6. 书写controller,并且完成交互
package cn.lazyfennec.springbootjsp.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
/**
* @Author Neco
* @Description TODO
*/
@Controller
public class WelcomeController {
@RequestMapping("/welcome")
public String toWelcome(HttpServletRequest servletRequest){
servletRequest.setAttribute("name","张三");
return "welcome";
}
}
7. 访问JSP页面,完成数据展示
访问JSP页面8. 更多内容,可以结合使用spel表达式等进行更多数据的展示
如果觉得有收获就点个赞吧,更多知识,请点击关注查看我的主页信息哦~
网友评论