静态页面

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";
}

最新文章

  1. Linux下Keepalived+LVS-DR模式配置高可用负载均衡集群
  2. EntityFramework之一对一关系(二)
  3. SQLite Design and Concepts
  4. JQ 操作 radio、checkbox 、select
  5. &lt;测试用例设计&gt;用户及权限管理功能常规测试方法
  6. 关于Eclipse插件开发(五)-----编辑器类方法的使用说明
  7. Hibernate中的多对多关系详解(3)​
  8. div+css页面居中代码
  9. 应用SVN(CentOS中搭建SVN服务器)
  10. UCOS 杂项 笔记
  11. python的pyc和pyo文件
  12. c# 坑人的发邮件组件
  13. findBugs安装
  14. sublime text3快速生成html头部信息
  15. luoguP4072 [SDOI2016]征途
  16. JAVA高并发系列
  17. mint-ui 输入框按下按键执行查询
  18. 【nlp】中文分词基础原则及正向最大匹配法、逆向最大匹配法、双向最大匹配法的分析
  19. 第五节 HTML&amp;CSS -- 关于浮动和清除浮动的解说,以及两个大坑不要踩
  20. 小米3移动版 分区 调整/合并教程(16GB/64GB)

热门文章

  1. mysql如何让自增id从1开始设置方法
  2. QT5:第一章 初始化
  3. @private@protected@public@package
  4. 酷炫的3D照片墙
  5. numpy的linspace使用详解
  6. NOIP模拟赛 机器人
  7. Golang 简单 http 代理转发
  8. k8s的Pod控制器
  9. salt 模板
  10. 前端之bootstrap