二、SpringBoot整合JSP
JSP LimitationsWhen running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.* With Jetty and Tomcat, it should work if you use war packaging. An executable war will work when launched with java -jar, and will also be deployable to any standard container. JSPs are not supported when using an executable jar.* Undertow does not support JSPs.* Creating a custom error.jsp page does not override the default view for error handling. Custom error pages should be used instead.
JSP限制:SpringBoot默认支持的视图并非JSP,而是诸如Thymeleaf等模板,但有些时候,我们还是想要使用JSP页面,那么我们也以这么做
在上一个工程的基础上
①,修改pom.xml,添加如下红色部分标注的内容
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.1.4.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.hnnz</groupId> 12 <artifactId>demo</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>SpringBootDemo</name> 15 <description>Demo project for Spring Boot</description> 16 17 18 <properties> 19 <java.version>1.8</java.version> 20 </properties> 21 22 23 <dependencies> 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-web</artifactId> 27 </dependency> 28 29 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-test</artifactId> 33 <scope>test</scope> 34 </dependency> 35 36 37 <!-- Compile --> 38 <dependency> 39 <groupId>javax.servlet</groupId> 40 <artifactId>jstl</artifactId> 41 </dependency> 42 <!-- Provided --> 43 <dependency> 44 <groupId>org.springframework.boot</groupId> 45 <artifactId>spring-boot-starter-tomcat</artifactId> 46 <scope>provided</scope> 47 </dependency> 48 <dependency> 49 <groupId>org.apache.tomcat.embed</groupId> 50 <artifactId>tomcat-embed-jasper</artifactId> 51 <scope>provided</scope> 52 </dependency> 53 </dependencies> 54 55 <build> 56 <plugins> 57 <plugin> 58 <groupId>org.springframework.boot</groupId> 59 <artifactId>spring-boot-maven-plugin</artifactId> 60 </plugin> 61 </plugins> 62 </build> 63 64 65 </project>
这些是使用jsp的必备组件
②,在application.propertites中添加以下内容
1 spring.mvc.view.prefix: /WEB-INF/jsp/ 2 spring.mvc.view.suffix: .jsp
与SpringMVC类似的,他们代表视图的前缀与后缀
③在java文件夹下新建文件夹webapp/WEB-INF/jsp,在jsp文件夹下新建index.jsp,目录结构如图所示:
在index.jsp里添加任意内容,例如:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isELIgnored="false" %> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 6 <title>error页面</title> 7 </head> 8 <body> 9 Hello,Spring Boot! 10 </body> 11 </html>
④,修改HelloController
1 package com.hnnz.demo.controller; 2 3 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.ResponseBody; 7 import org.springframework.web.bind.annotation.RestController; 8 9 10 @Controller 11 public class HelloController { 12 13 14 @RequestMapping("/hello") 15 @ResponseBody 16 public String hello(){ 17 return "Hello,Spring!"; 18 } 19 20 21 @RequestMapping("/index") 22 public String index() { 23 return "index"; 24 } 25 26 }
-
修改类注解@RestController为@Controller,使此类下的方法返回值不再是字符串,而是视图
-
当我们仍需要使一个方法返回字符串时,我们可以再为这个方法添加@ResponseBody注解
⑤,启动SpringBootDemoApplication的主方法,访问 http://localhost:8080/index 浏览器返回的视图如下: