SpringBoot添加MVC支持 & 静态文件

MySpringBoot模板支持配置
本文介绍了如何为MySpringBoot项目添加模板支持,通过配置Thymeleaf前缀和后缀,将模板文件放置在正确的位置,实现视图渲染。同时,还简述了支持静态文件的方法。

现在,我们来给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

直接按照这个做就行了。

 

转载于:https://my.oschina.net/qiangzigege/blog/906119

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值