本文是簡單總結一下SpringBoot使用Jsp的Demo。html
在早期作項目的時候,JSP是咱們常用的java服務器頁面,其實就是一個簡化servlet的設計,在本文開始以前,回顧一下jsp的幾大對象,如圖。java
而在如今SpringBoot框架流行的時代,SpringBoot推薦的模版引擎也轉變爲了thymeleaf,本文來介紹一下SpringBoot使用Jsp頁面。git
建立一個項目springboot_jsp,pom文件中加入tomcat-embed-jasper依賴以及jstl標籤庫的依賴,完整pom代碼以下:web
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dalaoyang</groupId>
<artifactId>springboot_jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>springboot_jsp</name>
<description>springboot_jsp</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
複製代碼
在src/main目錄下建立webapp文件夾,爲了迎合傳統的結構,在webapp下建立WEB-INF文件夾,而後在新建的WEB-INF文件夾內建立jsp文件夾。(具體結構能夠自行設計,無需按照這個demo來),目錄結構如圖spring
配置文件內配置剛剛新建的目錄,以及jsp文件後綴,配置以下:apache
server.port=8888
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
複製代碼
建立一個IndexController,裏面寫入一個跳轉方法,代碼以下:瀏覽器
package com.dalaoyang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @author dalaoyang
* @project springboot_learn
* @package com.dalaoyang.controller
* @email yangyang@dalaoyang.cn
* @date 2018/8/13
*/
@Controller
public class IndexController {
@GetMapping("/")
public String index(Model model){
model.addAttribute("name", "dalaoyang");
return "index";
}
}
複製代碼
在src/main/webapp/WEB-INF/jsp下面新建一個index.jsp,代碼以下:tomcat
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
Hello,${name}
</body>
</html>
複製代碼
啓動項目,在瀏覽器訪問http://localhost:8888/,顯示以下結果:springboot
源碼下載 :大老楊碼雲bash