一,转向
a.如图,在resource目录下分别新建两个html。
b.在地址栏分别直接访问两个网页,如:http://localhost:8080/one.html
问题:我们发现放在template下的文件夹无法访问,为什么?
因为springboot的默认配置,默认情况templates文件无法访问。当时也有解决办法,如下设置,然后我们就可以通过地址访问了
resource.static-locations:classpath:/static/,classpath:/templates/
C.Controller设置重定向
package com.example.dem2.controler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class controller {
@RequestMapping("/hei")
public String hello(){
return "redirect:one.html";
}
}
二、使用thymeleaf
a.配置thymeleaf
1.添加所需要的的依赖到pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
2.修改controller
package com.example.dem2.controler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class controller {
@RequestMapping("hello")
public String hh(){
return "two";
}
}
访问成功
注意:此时页面只能放在template下,如果需要改变和上面说的例子同理:
spring:
thymeleaf:
cache: false
prefix: classpath:/templates/
suffix: .html