SpringBoot内嵌了Tomcat服务器,可以通过java -jar的方式直接启动jar包程序,也可以通过tomcat服务器启动war包,那么jar包中如果想要让SpringBoot程序直接容后台return到某个HTML页面中,该怎么操作呢?

SpringBoot提供了对模板的支持,我们可以通过模板将程序从后台直接跳转到一个具体的页面中,本案例通过maven项目来创建jar包项目,方法如下:

1、创建maven工程,结构如下:

spring boot html直接跳转页面 springboot怎么跳转html_spring

2、pom文件中引入必须的JAR包,内容如下:

<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.test</groupId>
  <artifactId>springbootdemo2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.7.RELEASE</version>
		<!-- lookup parent from repository -->
		<relativePath/>
	</parent>
	
	<properties>
		<java.version>1.8</java.version>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
	</properties>
	
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<!-- 为什么不需要版本号,在parent中已经封装好了 -->
		</dependency>
	
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
	</dependencies>
</project>

此处引入了三个Jar包,分别是 spring-boot-starter-web、spring-boot-starter-thymeleaf和spring-boot-devtools,前两个Jar包是实现SpringBoot项目后台直接跳转HTML页面必须的Jar包,spring-boot-devtools是我的个人习惯,用于支持热部署。

3、src/main/java文件夹下创建启动类和一个controller类,src/main/resources文件夹下创建一个templates文件夹,templates文件夹下创建一个HTML页面和两个错误页,结构如下:

spring boot html直接跳转页面 springboot怎么跳转html_SpringBoot跳转html_02

SpringBoot从后台去找HTML页面时,默认是从templates文件夹下去找的,正如找静态文件默认从static文件夹下去找一样,所以此处我们不需要在application.properties或者application.yml文件中再配置视图处理器了,当然如果本地端口和8080冲突,记得修改端口,接着看看各个文件中的内容:

TestController.java:

package com.test.controler;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
	
	@RequestMapping("/getAllStudents.do")
	public String getAllStudents() {
		return "studentList";
	}
}

此处需要注意,注解一个是@Controller,如果用了RestController注解,那么就表示将字符串发送到浏览器了。 

Test.java:

package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Test {
	public static void main(String[] args) {
		SpringApplication.run(Test.class, args);
	}
}

studentList.html: 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	this is studentList html!
</body>
</html>

404.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	this is 404 html!
</body>
</html>

500.html: 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	this is 500 html!
</body>
</html>

 4、启动服务,在浏览器窗口输入:http://localhost:8080/getAllStudents.do,页面截图如下:

spring boot html直接跳转页面 springboot怎么跳转html_SpringBoot直接跳转到html_03

此时成功跳转到了我们的studentList.html页面中。

 

5、在浏览器窗口输入:http://localhost:8080/getAllStudents,页面截图如下:

 

spring boot html直接跳转页面 springboot怎么跳转html_SpringBoot跳转html_04

因为我们的后台方法中没有getAllStudents映射路径,所以找不到页面,系统自动为我们跳转到了404页面,同理,如果有一个映射路径,如果映射路径对应的后台方法有错误,那么系统也将自动为我们跳转到500页面。

 

以上为个人近段时间总结,之前一直在用jsp,对后台跳转到html页面不太了解,所以最近项目中一直用这种方式,如果不对之处,敬请指正!