SpringBoot框架:第二章:SpringBoot中static和templates二个目录下的页面和静态资源访问的三个常见问题

静态页面:

在resources建立一个static目录和index.htm静态文件,访问地址 http://localhost:8080/index.html
在这里插入图片描述

spring boot项目只有src目录,没有webapp目录,会将静态访问(html/图片等)映射到其自动配置的静态目录,如下

/static

/public

/resources

/META-INF/resources

如果要从后台跳转到静态index.html

@Controller
public class HtmlController {
@GetMapping("/html")
public String html() {
return “/index.html”;
}

动态页面:

使用Thymeleaf来做动态页面,在pom.xml 中添加Thymeleaf组件

<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-thymeleaf</artifactId>
    		</dependency>

templates目录为spring boot默认配置的动态页面路径

package hello;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.;
import org.springframework.web.bind.annotation.
;

@Controller
public class TemplatesController {

@GetMapping("/templates")
String test(HttpServletRequest request) {
	//逻辑处理
	request.setAttribute("key", "hello world");
	return "/index";
}  

}

index.html页面:

<!DOCTYPE html>
    <html>
    <span th:text="${key}"></span>
    </html>	

访问地址:http://localhost:8080/templates

问题来了

第一个是:启动项目之后,不需要进过后台,直接localhost:8080就可以直接访问templates中的index.html页面,不是访问static中的index.html页面,这个要怎么设置?

回答:正常途径应该是用nginx或apach代理服务器做跳转

更多请见:http://www.mark-to-win.com/tutorial/51204.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值