版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chen18677338530/article/details/90719268
JSP介绍
JSP(Java Server Pages)中⽂文名叫 Java 服务器器⻚页⾯面,其根本是⼀一个简化的 Servlet 设计。用 JSP 开发的 Web 应⽤用是跨平台的,既能在 Linux 下 运⾏行行,也能在其他操作系统上运行。
代码结构
修改pom文件,引入解析jsp的依赖
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.chen</groupId>
<artifactId>springboot-jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-jsp</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>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
修改application.yml文件
spring:
mvc:
view:
prefix: /WEB-INF/jsp/ # jsp文件存放的目录
suffix: .jsp # 文件后缀
根据yml文件新建路径和welcome.jsp文件
<!DOCTYPE html>
<html lang="zh">
<body>
time:${time}
<br/>
msg:${msg}
</body>
</html>
新建控制器
package com.chen.springbootjsp.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
@Controller
public class WelcomeController {
@GetMapping("/")
public ModelAndView welcome(){
ModelAndView modelAndView = new ModelAndView();
Calendar calendar = Calendar.getInstance();
modelAndView.addObject("msg","helo jsp");
modelAndView.addObject("time",calendar.get(Calendar.YEAR) + "-" +calendar.get(Calendar.MONTH) +"-" +calendar.get(Calendar.DATE));
modelAndView.setViewName("welcome");
return modelAndView;
}
}
运行结果