前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot框架:第二章:SpringBoot中static和templates二个目录下的页面和静态资源访问的三个常见问题

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

作者头像
马克社区
发布2022-09-28 16:53:58
4810
发布2022-09-28 16:53:58
举报
文章被收录于专栏:高端IT高端IT

静态页面:

在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组件

代码语言:javascript
复制
<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-thymeleaf</artifactId>
    		</dependency>
1234

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

package hello;

import javax.servlet.http.HttpServletRequest;

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

@Controller public class TemplatesController {

代码语言:javascript
复制
@GetMapping("/templates")
String test(HttpServletRequest request) {
	//逻辑处理
	request.setAttribute("key", "hello world");
	return "/index";
}  
123456

}

index.html页面:

代码语言:javascript
复制
<!DOCTYPE html>
    <html>
    <span th:text="${key}"></span>
    </html>	

12345

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

问题来了

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

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

更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/120382381

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档