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

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

时间:2022-09-28 18:25:21浏览次数:72  
标签:templates index 常见问题 SpringBoot 静态 html static 页面

静态页面:

在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代理服务器做跳转

 

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

 

标签:templates,index,常见问题,SpringBoot,静态,html,static,页面
From: https://www.cnblogs.com/wangchuanxinshi/p/16739123.html

相关文章

  • springboot 错误笔记
    1.springlogback控制台日志出现INFO类似错误显示字符:能够出现上面的原因是你的控制台输出工具不支持ANSI彩色字符,但是你的Spring配置文件中又......
  • SpringBoot框架:第一章:基础配置和介绍
    什么是SpringBoot?SpringBoot就是一些库的集合,它能够被任意项目的构建系统所使用,简化新Spring应用的初始搭建以及开发过程,简化配置,用更简单的办法整合第三方其他技术。......
  • springboot整合seata1.5.2+nacos2.1.1(未完成)
    一、前言Seata出现前,大部分公司使用的都是TCC或者MQ(RocketMq)等来解决分布式事务的问题,TCC代码编写复杂,每个业务均需要实现三个入口,侵入性强,RocketMQ保证的是最终一致......
  • 创建springboot+maven项目
    参考:https://www.cnblogs.com/ygfsy/p/13675122.html 目录结构-->在项目下创建一个Control包,用于返回给前端的接口   测试接口是否通 ......
  • SpringBoot 整合RabbitMq 实战
    SpringBoot整合RabbitMq实战参考官网:​​https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-messaging.html#boot-features-amqp​​spri......
  • Springboot 外置配置详解
    Springboot外置配置springBoot自动配置的bean提供了300多个用于微调的属性.当调整设置时,只需要在环境变量,Java系统属性,JNDI,命令行参数,属性文件进行配置就好了.举例......
  • 玩转SpringBoot之定时任务
    玩转SpringBoot之定时任务使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式:一、基于注解(@Scheduled)二、基于接口(SchedulingConfigurer)前者相信大家......
  • 深入学习SpringBoot
    1.快速上手SpringBoot1.1SpringBoot入门程序开发SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程1.1.1IDEA创建Sp......
  • SpringBoot热部署
    一、引言(devtools)在开发过程中,由于每次修改完项目中的类都需要重启服务才能看到运行的结果,对于开发调试很不友好,浪费时间,引入devtools工具可以快速启动项目,无需再次重启......
  • 计算机毕设选题新颖推荐 springboot+vue心理咨询预约系统 ssm校园心理咨询平台 java心
    ......