Spring Boot 2 中默认不使用JSP作为Web页面渲染的模板,但一些用JSP写的老系统升级时,还是希望能配置对JSP的支持。本文演示在Spring Boot 2中配置JSP。
新建WEB项目 在Spring Boot Starter ,填写项目的基本配置信息和项目中需要用到的第三方组件,根据实验的目标,选择用 maven 作为构建工具,Java 作为编程语言,Spring Boot 的版本选择当前的稳定版本,支持的包中,只需要选 择Spring Web Starter 即可。
下载以后,添加对 jsp 解析的支持,最终的 pom 文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 <?xml version="1.0" encoding="UTF-8"?> <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <parent > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-parent</artifactId > <version > 2.2.2.RELEASE</version > <relativePath /> </parent > <groupId > cn.com.hohistar.training</groupId > <artifactId > springboot-web-jsp-basic</artifactId > <version > 0.0.1-SNAPSHOT</version > <name > springboot-web-jsp-basic</name > <description > Demo project for Spring Boot</description > <properties > <java.version > 1.8</java.version > </properties > <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > org.apache.tomcat.embed</groupId > <artifactId > tomcat-embed-jasper</artifactId > <scope > provided</scope > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-test</artifactId > <scope > test</scope > <exclusions > <exclusion > <groupId > org.junit.vintage</groupId > <artifactId > junit-vintage-engine</artifactId > </exclusion > </exclusions > </dependency > </dependencies > <build > <plugins > <plugin > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-maven-plugin</artifactId > </plugin > </plugins > </build > </project >
添加Controller 每个HTTP请求都可以配置对应的Controller中的方法来响应。在案例中,我们建一个Controller来响应对根和hello的响应。
新建名为 controller 的包,在包中新建名为: HelloController 的类, 代码如下:
1 2 3 4 5 6 7 8 9 10 @Controller public class HelloController { @GetMapping ({"/" , "/hello" }) public String hello (Model model, @RequestParam(value="name" , required=false , defaultValue="World" ) String name) { model.addAttribute("name" , name); return "hello" ; } }
建立 jsp 页面 在 src/main 目录下新建一个 webapp 目录,再在 webapp目录下建立WEB-INF/jsp目录。最后在jsp目录下建立名为 hello.jsp 的文件,内容如下:
1 2 3 4 5 6 7 8 9 10 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" > <title > Hello #123;name}!</title > </head > <body > <h2 > Hello #123;name}!</h2 > </body > </html >
设置配置文件 将 src/main/resources 中的 application.properties 文件改名为: application.yml, 添加如下内容:
1 2 3 4 5 spring: mvc: view: prefix: /WEB-INF/jsp/ suffix: .jsp
为IDEA 开发工具设置环境 选择 File->Project Structure 在弹出的页面中选 Modules,中间一栏中展开项目,选Web(没有则按“+”号新建) 设置Deployment Descriptors和Web Resource Directories,其中Deployment Descriptors指向 <项目名称>/src/main/webapp/WEB-INF/web.xml,目前是没有web.xml的,会自动创建,Web Resource Directories 指向 <项目名称>/src/main/webapp/WEB-INF/ 设置完成后,就可以在 IDEA 中启动并调试项目。