SpringBoot下的Starter-web本身是没有支持模板引擎的,返回的页面也只能是静态资源,所以如果要将后台数据和页面一起返回需要增加模板引擎。
SpringBoot 返回视图404
SpringBoot 返回视图,静态资源报404一般是未指定资源路径。可以是使用以下两种方式解决。
1.方式1
指定静态资源路径
在application.yml修改
spring:
resources:
static-locations: classpath:/
2.方式2
修改pom文件,编译时指定资源路径。
修改pom编译时指定资源的目标路径
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<!--这里必须是META-INF/resources-->
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
模板引擎
除了REST Web服务之外,还可以使用Spring MVC来服务动态HTML内容。Spring MVC支持多种模板技术,包括Thymeleaf,FreeMarker,Groovy,Muslache和JSP。此外,许多其他模板引擎包括它们自己的Spring MVC集成。
1.JSP模板:
JSP限制:当运行使用嵌入的servlet容器的Spring Boot应用程序(并打包为可执行文件)时,JSP支持中存在一些限制。
- 有了Tomcat,如果你使用war包装,它应该可以工作。 也就是说,一个可执行的war工作,也可以部署到一个标准的容器(不限于,但包括Tomcat)。 由于Tomcat中的硬编码文件模式,可执行jar无法使用。
- 使用Jetty,如果您使用war包装,它应该可以工作。 也就是说,一个可执行的jar有效,并且也可以部署到任何标准容器。
- Undertow不支持JSP。
- 创建自定义error.jsp页面不会覆盖错误处理的默认视图。 应该使用自定义错误页面。
1.添加JSP支持的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--JSP模板引擎-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
2.返回视图的控制器
@Controller
@Slf4j
public class ViewController {
@GetMapping("/viewmodel")
public ModelAndView viewModel() {
log.info("viewModel Start");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("viewmodel");
modelAndView.addObject("message", "back end;无js");
return modelAndView;
}
@GetMapping("/viewmodeljs")
public ModelAndView viewModelJS() {
log.info("viewmodelJS Start");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("viewmodeljs");
modelAndView.addObject("message", "back end;js测试");
return modelAndView;
}
}
3.jsp视图
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>viewmodel</title>
</head>
<body>
<h1>hello ${message}</h1>
</body>
</html>
###############或者#####################
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>viewmodel-js</title>
<script src="../static/viewmodeljs.js" type="text/javascript"></script>
</head>
<body>
<h1>hello ${message}</h1>
<input onclick="onclickbtn()" type="button" value="button"/>
</body>
</html>
4.修改application.yml
指定spring.mvc匹配的资源前缀和后缀,如果不指定就需要在controller指定绝地路径和视图的后缀。
#返回的前缀 目录对应src/main/webapp下
spring:
mvc:
view:
prefix: /pages/
#返回的后缀
suffix: .jsp
5.pom文件build节点
这一步指定build将jsp资源编译到resources目录,如果不指定将会404错误。
<build>
<resources>
<resource>
<directory>src/main/mywebapp</directory>
<!--这里必须是META-INF/resources-->
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
6.启动调试
访问http://localhost:8080/viewmodeljs
2.thymeleaf模板
thymeleaf默认放置页面的目录是templates。
1.修改pom增加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf 模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
2.创建页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>viewmodel</title>
<script src="../mystatic/js/viewmodeljs.js"></script>
</head>
<body>
<div>
<h1 th:text="${message}"></h1>
<input value="button" type="button" onclick="onclickbtn()"/>
</div>
</body>
</html>
3.创建controller
@Controller
@Slf4j
public class ViewController {
@GetMapping("/viewmodel")
public ModelAndView viewModel() {
log.info("viewModel Start");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("viewmodel");
modelAndView.addObject("message", "back end;无js");
return modelAndView;
}
@GetMapping("/viewmodeljs")
public ModelAndView viewModelJS() {
log.info("viewmodelJS Start");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("viewmodeljs");
modelAndView.addObject("message", "back end;js测试");
return modelAndView;
}
}
4.修改自定义静态资源路径
resources:(Spring Boot 默认的)存放资源文件
static:(Spring Boot 默认的)存放静态文件,比如 css、js、image, (访问方式 http://localhost:8080/js/main.js)
public:(Spring Boot 默认的)存放公共文件
spring:
resources:
static-locations: classpath:/,classpath:/mystatic/