一、POM
<?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.yale</groupId>
<artifactId>SpringBootJsp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<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>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<!--springboot使用的web资源要编译到META-INF/resources-->
<resource>
<directory>src/main/webapps</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
</project>
二、application.yml
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
三、Controller
package com.yale.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
@Controller
public class IndexController {
@RequestMapping({"/","/index"})
public String index(Map<String,Object> map){
map.put("name","SpringBoot");
map.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
return "index";
}
}
四、Jsp
<%@page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Spring整合Jsp</title>
</head>
<body>
Hello ${name},当前时间:${date}.
</body>
</html>
五、附目录
D:\idea_workspace\springboot_test\SpringBootJsp>tree /f
文件夹 PATH 列表
卷序列号为 0000-8AEC
D:.
│ pom.xml
│ SpringBootJsp.iml
│
├─.idea
│
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─yale
│ │ │ │ Application.java
│ │ │ │
│ │ │ └─controller
│ │ │ IndexController.java
│ │ │
│ │ ├─resources
│ │ │ application.yml
│ │ │
│ │ └─webapps
│ │ └─WEB-INF
│ │ └─jsp
│ │ index.jsp
│ │
│ └─test
│ └─java
└─target
├─classes
│ │ application.yml
│ │
│ ├─com
│ │ └─yale
│ │ │ Application.class
│ │ │
│ │ └─controller
│ │ IndexController.class
│ │
│ └─META-INF
│ └─resources
│ └─WEB-INF
│ └─jsp
│ index.jsp
│...
D:\idea_workspace\springboot_test\SpringBootJsp>
六、补上Application
package com.yale;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
七、测试
1. Run -> Debug "spring-boot:run"
2. 浏览器访问:http://localhost:8080/
3. 返回:Hello SpringBoot,当前时间:2019-03-05 20:16:43.