发现thymeleaf 完全无效
看网上的视频学习的过程中,教程中使用的SpringBoot版本很多,有1.5.10有2.1.7还有自己使用的idea是2.2.x之后的版本,在通过网上查找资料发现,2.x之后版本变动较大,会和以前的有所不同,
在做静态资源映射的过程中出现的问题
比如在更改网站标题图片的时候,在2.2之前的版本中有faviconRequestHandler()方法,2.2.2版本中就没有,自己看源代码也比较费劲,有些不懂的问题需要自己网上搜索。
使用springboot 搭建了框架,然后再加入thymeleaf ,经过测试后发现thymeleaf 完全无效
错误:不能返回页面,只返回字符串。
<?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>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.harry</groupId>
<artifactId>springboottest03</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboottest03</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<!-- 布局功能的支持程序 thymeleaf3主程序 layout2以上版本 -->
<!-- thymeleaf2 layout1-->
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<!-- 引入web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入其他的Servlet容器-->
<!--<dependency>
<artifactId>spring-boot-starter-undertow</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--引入jquery-webjar-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
<!--引入bootstrap-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.0.0</version>
</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>
Controller层
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "Hello world";
}
@RequestMapping("/success")
public String success(){
//classpath:/templates/success.html
return "success";
}
}
无论返回 字符串 或者返回 ModelAndView,最终页面只显示了字符串,并没有跳去目标页面,一度以为的maven的依赖版本问题造成的,测试了很久,最终发现是Controller的问题。
因为在controller类中一直用的是@ResController这个注解,后来查了下资料发现:
官方文档:
@RestController is a stereotype annotation that combines @ResponseBody and @Controller.
意思是:
@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
1)如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
例如:本来应该到success.jsp页面的,则其显示success.
2)如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
3)如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。
原来,并没有集成失败 ,而是因为注解是 @RestController 配置的视图解析器InternalResourceViewResolver不起作用,所以返回的内容是字符串(就是Return 里的内容),把 @RestController 修改为 @Controller 后,视图解析器InternalResourceViewResolver才能成功调用返回指定页面
修改:修改注解为@Controller
修改后测试成功。