静态页面

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

/static

/public

/resources

/META-INF/resources

比如,在resources建立一个static目录和index.htm静态文件,访问地址 http://localhost:8080/index.html

如果要从后台跳转到静态index.html,代码如下。

[java] view plaincopyprint?
  1. @Controller
  2. public class HtmlController {
  3. @GetMapping("/html")
  4. public String html() {
  5. return "/index.html";
  6. }
@Controller
public class HtmlController {@GetMapping("/html")public String html() {return "/index.html";}

动态页面

动态页面需要先请求服务器,访问后台应用程序,然后再转向到页面,比如访问JSP。spring boot建议不要使用JSP,默认使用Thymeleaf来做动态页面。

在pom.xml  中添加Thymeleaf组件

[html] view plaincopyprint?
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. </dependency>
        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>

TemplatesController.java

[java] view plaincopyprint?
  1. package hello;
  2. import javax.servlet.http.HttpServletRequest;
  3. import org.springframework.stereotype.*;
  4. import org.springframework.web.bind.annotation.*;
  5. @Controller
  6. public class TemplatesController {
  7. @GetMapping("/templates")
  8. String test(HttpServletRequest request) {
  9. //逻辑处理
  10. request.setAttribute("key", "hello world");
  11. return "/index";
  12. }
  13. }
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";}
}  

@RestController:上一篇中用于将返回值转换成json

@Controller:现在要返回的是一个页面,所以不能再用@RestController,而用普通的@Controller/

request.setAttribute("key", "hello world"):这是最基本的语法,向页面转参数 key和value。

return "/index": 跳转到 templates/index.html动态页面,templates目录为spring boot默认配置的动态页面路径。

index.html    将后台传递的key参数打印出来

[html] view plaincopyprint?
  1. <!DOCTYPE html>
  2. <html>
  3. <span th:text="${key}"></span>
  4. </html>
<!DOCTYPE html>
<html>
<span th:text="${key}"></span>
</html>

访问http://localhost:8080/templates

这只是一个最基本的传参,templates标签和JSP标签一样,也可以实现条件判断,循环等各种功能。不过我在上一篇讲过,建议用静态html+rest替代动态页面,所以关于templates在此不做详细介绍

动态和静态区别

静态页面的return默认是跳转到/static/index.html,当在pom.xml中引入了thymeleaf组件,动态跳转会覆盖默认的静态跳转,默认就会跳转到/templates/index.html,注意看两者return代码也有区别,动态没有html后缀。

重定向

如果在使用动态页面时还想跳转到/static/index.html,可以使用重定向return "redirect:/index.html"。

[java] view plaincopyprint?
  1. @GetMapping("/html")
  2. public String html() {
  3. return "redirect:/index.html";
  4. }
  @GetMapping("/html")public String html() {return "redirect:/index.html";}

spring boot-html和templates相关推荐

  1. springboot templates读取不到_整合spring mvc + mybatis,其实很简单,spring boot实践(5)

    01 spring boot读取配置信息 02 多环境配置 03 处理全局异常 04 spring boot admin 主要通过spring boot整合spring mvc 以及mybatis实现 ...

  2. spring boot访问templates目录下的html静态页面

    spring boot访问静态资源:     1. static目录用来存放js.css.图片等静态资源. . .     2. templates目录用来存放html页面. . . spring b ...

  3. springboot templates读取不到_精通 Spring Boot 系列 04

    阅读全文,约 12 分钟这是江帅帅的第004篇原创 1. Web 开发的支持 使用 Spring Boot 实现 Web 开发更加便捷了,因为直接依赖 spring-boot-starter-web ...

  4. 狂神Spring Boot 员工管理系统 超详细完整实现教程(小白轻松上手~)

    [SpringBoot-web系列]前文: SpringBoot-web开发(一): 静态资源的导入(源码分析) SpringBoot-web开发(二): 页面和图标定制(源码分析) SpringBo ...

  5. springboot中文文档_登顶 Github 的 Spring Boot 仓库!艿艿写的最肝系列

    源码精品专栏 中文详细注释的开源项目 RPC 框架 Dubbo 源码解析 网络应用框架 Netty 源码解析 消息中间件 RocketMQ 源码解析 数据库中间件 Sharding-JDBC 和 My ...

  6. spring boot 实战 / 可执行war启动参数详解

    概述   上一篇文章<spring boot 实战 / mvn spring-boot:run 参数详解>主要讲解了spring boot 项目基于maven插件启动过程中借助profil ...

  7. Spring Boot thymeleaf模版支持,css,js等静态文件添加

    Thymeleaf引入 Thymeleaf是一个Java模板引擎开发库,可以处理和生成HTML.XML.JavaScript.CSS和文本,在Web和非Web环境下都可以正常工作. 1.添加依赖包 & ...

  8. Spring Boot 整合 Freemarker

    这是一个相当老牌的开源的免费的模版引擎.通过 Freemarker 模版,我们可以将数据渲染成 HTML 网页.电子邮件.配置文件以及源代码等.Freemarker 不是面向最终用户的,而是一个 Ja ...

  9. spring boot+oracle数据库配置成功

    第一次配这个,网上找数据库不是oracle ,实验了2天. 那啥,第一次接触,里面的东西几乎不懂. 文章目录 maven依赖 类 baseBean类 user1类 userController类 Co ...

  10. Spring Boot 前后端配合及接口化测试学习记录[3]

    前后端不分离 1.对静态资源的映射处理 2.模板引擎: 模板引擎(这里特指用于Web开发的模板引擎)是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成 ...

最新文章

  1. CRM系统业务的分析(1)
  2. 学习数字图像处理经验谈
  3. javafx css_JavaFX技巧13:研究Modena CSS文件
  4. 语言统计学中的几个定律,可作为设计检索的参考
  5. android按钮置于顶层,如何把按键显示在最顶层窗口上(屏幕最顶上)
  6. HTML转义字符大全<转>
  7. 特斯拉联合苹果发难 要对小鹏汽车“窃密”员工动手了...
  8. 【资源分享】分享十个历史版本的eclipse安装包
  9. iis打不开php,php出现404找不到网页错误 iis配置问题解决
  10. 计算机usb接口失灵,如何解决电脑USB接口失灵的问题
  11. 【可达编程】P0063. 小武老师的烤全羊
  12. 数据预处理Part5——样本分布不均衡
  13. 工作量单位-人月、人日、人时 详解
  14. 解决SAS/EG中某些process出现的WARNING: The font Arial is not available. Albany AMT will be used.
  15. ios 开发设置左滑退出_iOS 开发UITableView左滑出现删除按钮的运用方法
  16. openssh linux 下载,OpenSSH 下载与配置
  17. 如何用人工智能预测股票(完整答案)
  18. erp中的:ATP、CTP、APS的概念
  19. ubuntu中的apt终端命令解析
  20. [转]caoz写的关于系统分析的文章

热门文章

  1. 【ACM夏训】综合训练赛
  2. 文件怎么更新_干货!Win10更新总失败?学会这三招搞定它
  3. 2021-09-07912. 排序数组
  4. 297.二叉树的序列化与反序列化
  5. java语言没有保留结构和联合,java选择题判断题题库.doc
  6. Spring Cloud学习笔记---Spring Cloud Sleuth--一个手动搭建zipkin碰到的坑
  7. mysql5.7 字符集编码
  8. 简单易学!一步步带你理解机器学习算法——马尔可夫链蒙特卡罗(MCMC)
  9. 编译原理完整学习笔记(一):引论
  10. 【POJ 2449】第K短路【A*算法】