-
新建springboot工程:springboot_jsp,打包方式为war
-
导入web模块:
生成的项目结构:
包含启动类SpringbootJspApplication:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootJspApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootJspApplication.class, args);
}
}
ServletInitializer类:
package com.example.demo;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringbootJspApplication.class);
}
}
pom.xml:
<?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.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot_jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</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>
- 在pom文件中添加支持jsp的依赖:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
- 在 webapp 文件夹下新建 WEB-INF 文件夹,在 WEB-INF 下新建 test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>success</h1>
</body>
</html>
- 新建HelloController类
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "test";
}
}
- application.properties文件:
server.port=9000
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
-
将项目添加到tomcat容器中,启动tomcat,访问路径:http://localhost:9000/springboot_jsp/hello,页面显示失败,再访问路径:http://localhost:8080/springboot_jsp/hello,页面成功显示:
这说明,使用外部tomcat时,在配置文件中配置的端口号server.port失效了。 -
修改test.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>success</h1>
<br>
<a href="click">click</a>
</body>
</html>
- 新建second.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>${msg }</h1>
</body>
</html>
- 新增请求:
@GetMapping("/click")
public String click(Model model) {
model.addAttribute("msg", "click---second.jsp");
return "second";
}
- 重启tomcat,请求http://localhost:8080/springboot_jsp/hello
- 点击click链接,跳转至second.jsp,从Model中传递过来的值可正常显示:
总结:
- 创建一个 springboot/maven 工程,打包方式为war包
- 要有一个ServletInitializer的类(继承SpringBootServletInitializer)
- 在pom文件中增加jsp的依赖,修改tomcat启动器的scope
- 调整目录结构,jsp页面要放在WEB-INF下,在application.properties/yml中配置页面的前后缀(视图解析)
- 启动服务时,使用的是外部的容器