Spring Boot 初级入门教程(九) —— 添加 JSP 支持
2018年07月25日 19:14:50 SpringBoot ⁄ 共 2388字 暂无评论 ⁄ 被围观 3,444次

大多数 WEB 开发,都还是用的 JSP 页面,所以如何让 SpringBoot 项目支持 JSP,这篇简单说一下。

一、需要引入依赖的 jar 包。

查看 pom.xml 文件中是否引入下面的 jar 包,如果没有引用,则需要引用才行。

		<!-- 该依赖包提供了MVC、AOP等的依赖包 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<version>2.0.2.RELEASE</version>
		</dependency>

		<!-- 添加JSP页面支持 -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<version>8.5.31</version>
			<scope>compile</scope>
		</dependency>

二、修改配置文件。

# 配置访问页面的前缀
spring.mvc.view.prefix=/WEB-INF/pages/
# 配置访问页面的后缀
spring.mvc.view.suffix=.jsp

指定 JSP 文件存放的路径以及文件后缀名。

三、添加源码目录。

创建 src/main/webapp 源码目录,其下依次创建 WEB-INF/pages/*.jsp。如图:

四、编写 JSP 页面文件。

testJspPage.jsp 内容为:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>TEST JSP PAGE</title>  
  5.     <meta http-equiv="content-type" charset="UTF-8">  
  6.     <meta name="description" content="JSP测试页面" />  
  7.     <style type="text/css">  
  8.         table {  
  9.             width: 50%;  
  10.             border-collapse: collapse;  
  11.         }  
  12.           
  13.         table, th, td {  
  14.             border: 1px solid black;  
  15.         }  
  16.           
  17.         .redfont {  
  18.             color: red;  
  19.         }  
  20.     </style>  
  21. </head>  
  22.   
  23. <body>  
  24.     <table>  
  25.         <tr>  
  26.             <th>DESC</th>  
  27.             <th>VALUE</th>  
  28.         </tr>  
  29.         <tr>  
  30.             <td>String Value From application.properties</td>  
  31.             <td class="redfont">${testSpringCfgStr}</td>  
  32.         </tr>  
  33.     </table>  
  34. </body>  
  35. </html>  

五、添加测试类。

新建测试类 JspPageController.java,内容如下:

  1. package com.menglanglang.test.springboot.controller;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import org.springframework.beans.factory.annotation.Value;  
  6. import org.springframework.stereotype.Controller;  
  7. import org.springframework.web.bind.annotation.RequestMapping;  
  8.   
  9. /** 
  10.  * @desc JSP页面控制类 
  11.  * 
  12.  * @author 孟郎郎 
  13.  * @blog http://blog.csdn.net/tzhuwb 
  14.  * @version 1.0 
  15.  * @date 2018年7月25日下午5:43:03 
  16.  */  
  17. @Controller  
  18. @RequestMapping("/jsp")  
  19. public class JspPageController {  
  20.   
  21.     @Value("${com.mll.constant.val.str}")  
  22.     public String testSpringCfgStr;  
  23.   
  24.     /** 
  25.      * 测试Spring配置文件定义的常量字符串 
  26.      *  
  27.      * @return 
  28.      */  
  29.     @RequestMapping("/testJspPage")  
  30.     public String testJspPage(Map<String, Object> map) {  
  31.         map.put("testSpringCfgStr", testSpringCfgStr);  
  32.         return "testJspPage";  
  33.     }  
  34.   
  35. }  

六、测试

启动项目,在浏览器中访问 http://localhost:8080/jsp/testJspPage,结果如下:

到此,JSP 支持添加完毕。

原文链接:https://blog.csdn.net/tzhuwb/article/details/81208337

给我留言

留言无头像?