Thymeleaf默认界面为index.html
如果在templates中存在index.html,输入地址http://localhost:8080/会跳转到index.html
如果没有就报错404
源码
在源码中有这样的方法
- welcomePageHandlerMapping:返回首页处理器映射
- getWelcomePage:获得首页,在静态资源resourceProperties类中寻找首页
- getIndexHtml:在规定的静态资源放置文件夹下,寻找index.html
静态资源可以放在:
- classpath:/META-INF/resources/
- classpath:/resources/
- classpath:/static/
- classpath:/public/
classpath是指:resources目录
自定义首页
网上查不到像web.xml里那样设置<welcome-file-list>
那么简单的
只找到页面跳转的憨批方法。。。
注意要导入模板,前端页面需要解析器
package com.test.springboottest.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class FirstHtmlController {
@RequestMapping("/")
public String toFirstHtml(){
return "NoIndex";
}
}
这个页面需要放置在templates目录下
设置一个控制器@RequestMapping("/"),return想要的界面
输入地址http://localhost:8080/就跳到了我想要的界面
为什么要在themplates文件夹下?
因为在源码中默认设置了/templates
当然也可以自定义:在application.yml里
比如:
spring:
thymeleaf:
prefix: /public