在Spring Boot中访问静态页面有以下几种方法: 在src/main/resources/static目录下创建静态页面文
在Spring Boot中访问静态页面有以下几种方法:
在src/main/resources/static
目录下创建静态页面文件,直接通过URL访问即可。例如,如果有一个名为index.html的静态页面文件,可以通过Http://localhost:8080/index.html
访问。
在src/main/resources/templates
目录下创建Thymeleaf模板文件,可以使用Thymeleaf模板引擎渲染页面,然后返回给浏览器。这种方式可以将动态数据传递给页面进行渲染。例如,如果有一个名为index.html的Thymeleaf模板文件,可以通过创建一个Controller来返回该页面:
@Controller
public class MyController {
@GetMapping("/index")
public String getIndexPage() {
return "index";
}
}
然后通过http://localhost:8080/index
来访问。
@Controller
注解和@RequestMapping
注解创建一个Controller,在方法中使用ModelAndView
类来设置要返回的页面和数据。例如:@Controller
public class MyController {
@RequestMapping("/")
public ModelAndView home() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
}
然后通过http://localhost:8080/
来访问。
需要注意的是,以上方法中的index
表示页面的名称,对应的是文件名(不含后缀)。如果spring Boot使用的是默认的Thymeleaf模板引擎,页面文件的后缀应为.html
;如果使用的是其他模板引擎,如FreeMarker或Velocity,相应地修改文件后缀名。
--结束END--
本文标题: springboot怎么访问静态页面
本文链接: https://www.lsjlt.com/news/570907.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0