1、SpringBoot整合之SpringBoot整合JSP
2021/7/2 23:21:43
本文主要是介绍1、SpringBoot整合之SpringBoot整合JSP,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
SpringBoot整合JSP
一、创建SpringBoot项目,仅选择Web模块即可
二、在POM文件中添加依赖
<!-- 添加servlet依赖模块 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <!-- 添加jstl标签库依赖模块 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!--添加tomcat依赖模块.--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <!-- 使用jsp引擎,springboot内置tomcat没有此依赖 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency>
三、创建目录:webapp/WEB-INF、webapp/jsps
注意:一定要在main目录下,与 /java 同级
四、修改步骤三中的目录结构
五、在核心配置文件中添加视图解析器的前缀和后缀
文件位置:src/main/resources/application.properties
spring.mvc.view.prefix=/jsps/ //设定视图解析器的前缀,这里的前缀根据你jsp文件的位置进行选择 spring.mvc.view.suffix=.jsp //设定视图解析器的后缀
六、创建JSP
文件位置:webapp/jsps/test.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Title</title> </head> <body> <h1>Hello, test.jsp</h1><br/> 1+1=${1+1}<br/> ${requestScope.time}<br/> </body> </html>
七、创建一个action进行测试
package cn.byuan.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import java.text.SimpleDateFormat; import java.util.Date; @Controller @RequestMapping("/test") public class JavaServicePagesTest { private static final String datePattern="yyyy-MM-dd E HH:mm:ss"; @RequestMapping("/m1.action") public String methodOne(HttpServletRequest request){ request.setAttribute("time", new SimpleDateFormat(datePattern).format(new Date())); return "test"; } }
八、配置web resources directorys
九、运行项目,打开浏览器输入URL
http://localhost:8080/text/m1.action
源码地址:https://github.com/byuan98/springboot-integration/tree/master/test001_springboot_jsp
这篇关于1、SpringBoot整合之SpringBoot整合JSP的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?