JavaServer Pages(JSP)技术使Web开发人员和设计人员可以快速开发和轻松维护利用现有业务系统的信息丰富的动态Web页面。html
做为Java技术系列的一部分,JSP技术能够快速开发独立于平台的基于Web的应用程序。JSP技术将用户界面与内容生成分开,使设计人员可以在不改变底层动态内容的状况下更改总体页面布局。java
对开发人员的好处
若是您是熟悉HTML的网页开发人员或设计人员,则能够:git
——以上内容摘自自 Oracle 关于JSP的介绍 连接地址:https://www.oracle.com/technetwork/java/overview-138580.htmlgithub
虽然如今 JSP基本已经淘汰,可是不少公司的老的项目仍是在用 JSP 做为页面,经过阅读该篇博客,你将了解到如何在SpringBoot 中快速使用 JSP 简单操做。web
第一步在 pom.xml 添加支持 JSP 视图的依赖,具体代码以下:spring
<!-- 非必选 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- Provided 编译和测试的时候使用--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!-- 对jsp的支持的依赖 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
第二步在 application.properties 配置文件中添加 JSP 相关配置信息,具体配置信息以下:apache
server.port=8080 server.servlet.context-path=/sbe spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
第三步建立 src/main/webapp 目录并在该目录建立 JSP。segmentfault
添加JSP文件的路径位置以下图所示:tomcat
JSP 文件内容以下:springboot
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> ${welcome} </body> </html>
第四步建立访问 JSP 页面的 Controller。
package cn.lijunkui.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping() public class JspController { @RequestMapping("/helloworld") public String toJps(Model model) { model.addAttribute("welcome", "不建议使用jsp"); return "welcome"; } }
在游览器输入访问 JSP 页面的 Controller 的 URL:http://localhost:8080/sbe/helloworld 进行测试,测试结果以下:
SpringBoot 使用 JSP 步骤以下:
具体代码示例请查看个人 GitHub 仓库 springbootexamples 中模块工程名: spring-boot-2.x-jsp 进行查看
Github:https://github.com/zhuoqianmingyue/springbootexamples
若是您对这些感兴趣,欢迎 star、或点赞给予支持!转发请标明出处!