Spring Boot (4) 静态页面和Thymeleaf模板
静态页面
spring boot项目只有src目录,没有webapp目录,会将静态访问(html/图片等)映射到其自动配置的静态目录,如下
/static
/public
/resources
/META-INF/resources
在resources建立一个static目录和index.html静态文件,访问地址就是 http://localhost:8080/index.html
动态页面
动态页面需要先请求服务器,访问后台应用程序,然后再转向到页面,比如访问jsp,spring boot建议不要使用jsp,默认使用Thymeleaf来做动态页面。
在pom.xml中添加Thymeleaf组件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
TemplatesController.java
@Controller //返回的是页面所以用简单的controller
public class TemplatesController {
@GetMapping("/templates")
String test(HttpServletRequest request) {
request.setAttribute("key", "hello world");
//跳转到templates/index.html动态页面,templates目录是spring boot 默认配置的动态页面路径
return "index";
}
}
templates/index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<span th:text="${key}"></span>
</body>
</html>
访问http://localhost:8088/dev/templates
这是一个最基本的像页面传递参数,templates标签和jsp标签一样,也可以实现条件判断,循环等各种功能,建议使用静态html+rest代替动态页面。
遍历数据:
@GetMapping("/template")
String template(HttpServletRequest request){
request.setAttribute("key",bookName + bookAuthor);
request.setAttribute("list", Arrays.asList("a","b","c","d","e"));
return "index";
}
遍历
<table>
<tr th:each="item : ${list}">
<td th:text="${item}"></td>
</tr>
</table>
数据判断:
<tr th:each="item : ${list}">
<td th:text="${item}" th:if="${item == 'a'}"></td>
</tr>
等于a才显示出来
<table th:if="${list != null && list.size() > 0}">
有数据才遍历
在javascript中访问model数据
<script>
var a = '[[${key}]]';
alert(a);
</script>
Spring Boot (4) 静态页面和Thymeleaf模板的更多相关文章
- Spring Boot获取前端页面参数的几种方式总结
Spring Boot的一个好处就是通过注解可以轻松获取前端页面的参数,之后可以将参数经过一系列处理传送到后台数据库. 获得的方式有很多种,这里稍微总结一下,大致分为以下几种: 1.指定前端url请求 ...
- Spring boot 默认静态资源路径与手动配置访问路径的方法
这篇文章主要介绍了Spring boot 默认静态资源路径与手动配置访问路径的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下 在application.propertis中配置 ##端口号 ...
- Spring Boot中静态资源(JS, 图片)等应该放在什么位置
Spring Boot的静态资源,比如图片应该放在什么位置呢, 如果你放在传统WEB共的类似地方, 比如webapp或者WEB-INF下,你会得到一张示意文件未找到的破碎图片.那应该放哪里呢? 百度一 ...
- 不常见偏门的Bug,Spring Boot IDEA 静态资源 图片访问404,初学者之殇
用过Idea朋友都知道,它有一个非常让人喜欢的功能就是:打算在某个a目录下创建一个hello.class文件,那么你仅需要右键点击New-Java Class- 然后输入名字:a.hello 即可. ...
- Spring Boot 的静态资源处理
做web开发的时候,我们往往会有很多静态资源,如html.图片.css等.那如何向前端返回静态资源呢?以前做过web开发的同学应该知道,我们以前创建的web工程下面会有一个webapp的目录,我们只要 ...
- spring boot 开静态资源访问,配置视图解析器
配置视图解析器spring.mvc.view.prefix=/pages/spring.mvc.view.suffiix= spring boot 开静态资源访问application.proerti ...
- spring boot 配置静态路径
一 前言 最近有个项目,需要上传一个zip文件(zip文件就是一堆的html压缩组成)的压缩文件,然后后端解压出来,用户可以预览上传好的文件. 查看资料,spring boot对静态文件,可以通过配 ...
- Spring Boot开发Web应用之Thymeleaf篇
前言 Web开发是我们平时开发中至关重要的,这里就来介绍一下Spring Boot对Web开发的支持. 正文 Spring Boot提供了spring-boot-starter-web为Web开发予以 ...
- Spring Boot 设置静态资源访问
问题描述 当使用spring Boot来架设服务系统时,有时候也需要用到前端页面,当然就不可或缺地需要访问其他一些静态资源,比如图片.css.js等文件.那么如何设置Spring Boot网站可以访问 ...
随机推荐
- react typescript 父组件调用子组件
//父组件import * as React from 'react'import { Input } from 'antd'const Search = Input.Searchimport &qu ...
- pandas - 案例(美国2012年总统候选人政治献金数据分析)
# 提供数据 months = {'JAN' : 1, 'FEB' : 2, 'MAR' : 3, 'APR' : 4, 'MAY' : 5, 'JUN' : 6, 'JUL' : 7, 'AUG' ...
- 第三节:Web爬虫之BeautifulSoup解析库
Beautiful Soup官方说明: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功能.它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为 ...
- PAT 1100. Mars Numbers
People on Mars count their numbers with base 13: Zero on Earth is called "tret" on Mars. T ...
- [luoguP1816] 忠诚(st表 || 线段树)
传送门 其实我就是想练练 st表 本以为学了线段树可以省点事不学 st表 了 但是后缀数组中用 st表 貌似很方便 所以还是学了吧,反正也不难 ——代码 #include <cstdio> ...
- 洛谷 P1586 四方定理
P1586 四方定理 题目描述 四方定理是众所周知的:任意一个正整数nn,可以分解为不超过四个整数的平方和.例如:25=1^{2}+2^{2}+2^{2}+4^{2}25=12+22+2 ...
- python getaddrinfo 函数
现在python中用到的关于地址查询的函数几乎都可以用getaddrinfo. 也就是说,如果你要想做一些与地址查询,主机名ip转换的操作,都可以用这个函数,下面看一下这个函数. 首先,我们可以用ge ...
- Android使用GestureDetector实现手势滑动效果
直接看实例: package com.example.gesturedetector; import android.os.Bundle; import android.app.Activity; i ...
- poj 1068 Parencodings(模拟)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj ...
- vbs use
VBScript中SendKeys的妙用 标签: vbscriptbasicmicrosoftinsertdeletestring 2011-05-26 15:29 1830人阅读 评论(0) 收藏 ...