springboot整合jsp实战教程
在早些年的时候,前后端分离开发还没有大范围流行的时候,常常是后端程序员一手包办前后台开发的工作,在采取这样开发模式的项目中,一般都会采用jsp作为显示页面的模板,以来是应为jsp本身既能写HTML代码,又能在其中写逻辑代码的交互,在这样优势的前提下,jsp一度成为前后端开发没有分离时页面展示层的香饽饽。虽然随着时代的发展,现在前后端已经广泛采用前后端开发的模式,但是jsp还是需要广大后端研发工程师需要掌握的一们模板语言,虽然可能不用,但是却不能不掌握,就像原子弹、氢弹一样,虽然造出来就没有使用过,但是这东西却不能没有。
工程目录结构
springboot-jsp
src
main
java
controller
Test.java
CodingApplication.java
resources
application.yml
webapp
WEB-INF
welcome.jsp
test
maven依赖配置
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
application.yml配置
server:
port: 8080
spring:
mvc:
view:
suffix: .jsp
prefix: /WEB-INF/jsp/
controller内容
@Controller
@RequestMapping("/test")
public class Test {
@RequestMapping("/welcome")
public String welcome(Map<String, Object> map) {
map.put("time", new Date());
map.put("message", "hello world");
return "welcome";
}
}
页面层内容(welcome.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title</title>
</head>
<body>
Time: ${time} <br/>
Message: ${message}
</body>
</html>
404原因排查
- 工程结构是否正确(webapp存在main包下)
- yml配置文件内容是否正确,springboot 1.x和2.x存在区别
- 访问的路径是否正确
- maven中添加的依赖是否正确
访问
http://localhost:8080/test/welcome