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代理服务器做跳转
更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/120382381
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?