现在,我们来给MySpringBoot添加模板支持
首先写一个Controller
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/mvc")
public class MvcController {
@RequestMapping(value = "/{name}", method = { RequestMethod.GET })
public String handle(@PathVariable("name") String name, Model model) {
model.addAttribute("name", name);
return "hello";
}
}
浏览器中输入: http://localhost:9990/mvc/liuzhiqiang
出现错误
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun May 21 11:17:14 CST 2017
There was an unexpected error (type=Internal Server Error, status=500).
Circular view path [hello]: would dispatch back to the current handler URL [/mvc/hello] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
那么,肯定是哪里没有配置好,其实上面返回的hello字符串并不是直接返回给浏览器,而是需要web容器找到hello的模板进行渲染。
下面我们来参考:http://www.tuicool.com/articles/Efy67re来进一步操作,
发现这篇文章不是很好,参考另外一篇
http://www.cnblogs.com/shiddong/p/5581906.html 继续处理
最后,经验就是:
配置文件中添加
spring.thymeleaf.prefix=classpath:/templates/thymeleaf/
spring.thymeleaf.suffix=.html
把模板文件放到src/main/resources/templates/thymeleaf文件夹下
再访问,就可以了。
===下面讲解如何支持静态文件
仍然是参考:http://www.cnblogs.com/shiddong/p/5581906.html
直接按照这个做就行了。