静态页面

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

/static

/public

/resources

/META-INF/resources

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

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

  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组件

  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

  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参数打印出来

  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"。

  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. Spring boot 直接访问templates中html文件

    application.properties 在浏览器中输入http://localhost:8080/index.html 会报一个 因为Spring boot 无法直接访问templates下的文 ...

  2. spring boot(4)-html和templates

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

  3. 玩转spring boot——结合redis

    一.准备工作 下载redis的windows版zip包:https://github.com/MSOpenTech/redis/releases 运行redis-server.exe程序 出现黑色窗口 ...

  4. 玩转spring boot——AOP与表单验证

    AOP在大多数的情况下的应用场景是:日志和验证.至于AOP的理论知识我就不做赘述.而AOP的通知类型有好几种,今天的例子我只选一个有代表意义的“环绕通知”来演示. 一.AOP入门 修改“pom.xml ...

  5. 玩转spring boot——结合JPA入门

    参考官方例子:https://spring.io/guides/gs/accessing-data-jpa/ 接着上篇内容 一.小试牛刀 创建maven项目后,修改pom.xml文件 <proj ...

  6. 玩转spring boot——结合JPA事务

    接着上篇 一.准备工作 修改pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

  7. 玩转spring boot——结合AngularJs和JDBC

    参考官方例子:http://spring.io/guides/gs/relational-data-access/ 一.项目准备 在建立mysql数据库后新建表“t_order” ; -- ----- ...

  8. 玩转spring boot——MVC应用

    如何快速搭建一个MCV程序? 参照spring官方例子:https://spring.io/guides/gs/serving-web-content/ 一.spring mvc结合thymeleaf ...

  9. 第一个Spring Boot Web程序

    需要的环境和工具: 1.Eclipse2.Java环境(JDK 1.7或以上版本)3.Maven 3.0+(Eclipse已经内置了) 写个Hello Spring: 1.新建一个Maven项目,项目 ...

  10. Spring boot配置文件 application.properties

    http://www.tuicool.com/articles/veUjQba 本文记录Spring Boot application.propertis配置文件的相关通用属性 # ========= ...

随机推荐

  1. java基础—java制作证书的工具keytool

    一.keytool的概念 keytool 是个密钥和证书管理工具.它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认证自己)或数据完整性以及认证服务.在 ...

  2. java基础—线程(二)

    一.线程的优先级别

  3. Linux下Jenkins与GitHub自动构建Node项目(Vue)

    根据上篇文章<Linux下Jenkins与GitHub自动构建NetCore与部署>,我们知道了Jenkins的强大功能,自动构建,部署了一个NetCore的Web,让开发人员专注于开发, ...

  4. LiteIDE 错误: go build xxxxxx: no non-test Go files in xxxx

    问题 c:/go/bin/go.exe build [C:/Users/Administrator/Desktop/go] go build _/C_/Users/Administrator/Desk ...

  5. Maven各种常用架包配置文件,保存一份

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. 【dp】P1982 小朋友的数字

    有趣的细节题目 题目描述 有 n 个小朋友排成一列.每个小朋友手上都有一个数字,这个数字可正可负.规定每个小朋友的特征值等于排在他前面(包括他本人)的小朋友中连续若干个(最少有一个)小朋友手上的数字之 ...

  7. Python数据分析实战视频教程【小蚊子数据分析实战课程】

    点击了解更多Python课程>>> Python数据分析实战视频教程[小蚊子数据分析实战课程] [课程概述] Python数据分析实战' 适用人群:适合需提升竞争力.提升工作效率.喜 ...

  8. REST Framework 处理一个超链接序列化问题

    问题简述 翻译: 不正确的配置 无法使用视图名称“snippet-detail”解析超链接关系的URL.您可能没有在API中包含相关的模型,或者在该字段上错误地配置了' lookup field '属 ...

  9. GPIO程序在PC上的模拟学习

    #include <stdio.h> #include <malloc.h> #include <memory.h> typedef struct gpio { i ...

  10. Python动态属性和特性(一)

    在Python中,数据的属性和处理数据的方法统称为属性.其实,方式只是可调用的属性.除了这二者之外,我们还可以创建特性(property),在不改变类接口的前提下,使用存取方法(即读取值和设置值方法) ...