Spring Boot 的简单教程(三) web页面开发(JSP篇)

上个章节咱们讲了web页面开发的Thymeleaf开发。
如今咱们就须要说一下咱们之前经常使用的JSP页面开发了,由于JSP没法实现Spring Boot的多种特性,因此Spring Boot不推荐使用JSP进行页面开发。html

JSP页面开发

第一,须要在pom.xml中添加依赖文件。

<!--WEB支持-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--jsp页面使用jstl标签-->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

<!--用于编译jsp-->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

第二,在application.yml中配置返回文件的路径以及类型:

#这里是端口号
server.port=8088
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

第三,在main下面新建一个webapp文件夹,下面新建WEB-INF文件夹,在下面新建一个jsp文件夹,将页面放到jsp文件夹下面便可。

clipboard.png

index.jsp页面内容:java

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>测试页面</title>
</head>
<body>
    <h1>HELLO WORLD!!!</h1>
</body>
</html>

第四,正常的书写Controller方法便可。

clipboard.png

第五,访问http://localhost:8088/index成功。

clipboard.png

相关文章
相关标签/搜索