static和templates部分参考博客:https://blog.csdn.net/wangb_java/article/details/71775637

热部署参考博客:https://www.cnblogs.com/cx-code/p/8686453.html

SpringBoot里面没有我们之前常规web开发的WebContent(WebApp),它只有src目录

在src/main/resources下面有两个文件夹,static和templates   springboot默认  static中放静态页面,而templates中放动态页面

静态页面:

这里我们直接在static放一个hello.html,然后直接输入http://localhost:8080/hello.html便能成功访问

(好像可以新建一个public文件夹,也可以放静态文件)

也可以通过controller跳转:

@Controller
public class HelloController { @RequestMapping("/Hi")
public String sayHello() {
return "hello.html";
} }

然后输入http://localhost:8080/Hi就可以成功访问

动态页面:

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

现在pom中要添加Thymeleaf组件

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

我们先在tempates文件夹中也新建一个hello.html但内容不同,然后先试一下直接访问该页面。输入http://localhost:8080/hello.html:

结果显然访问的是静态问价夹里面的那个hello.html

然后我们现在再试一下用controller:

似乎无法访问到hello.html了。。。这是因为:

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

也就是我们要这样改controller:

@Controller
public class HelloController { @RequestMapping("/Hi")
public String sayHello() {
return "hello";
} }

然后就可以成功跳转了

然后我们看看返回一点数据在前端利用Thyemleaf来拿:

@Controller
public class HelloController { @RequestMapping("/Hi")
public ModelAndView sayHello() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("hello");
modelAndView.addObject("key", 12345);
//System.out.println("test");
return modelAndView;
} }
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Insert title here</title> </head>
<body>
<h1>this is the hello.html in templates</h1>
<span th:text="${key}"></span>
</body>
</html>

效果:

如果不想返回视图,则用@RestController

如果用了静态模板你还想返回static中的页面,那么就要用重定向:

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

return "redirect:hello.html";  

几点tips:

1.拦截的url最后不要跟视图重合,否则会抛出Circular view path异常,我之前就是

@Controller
public class HelloController { @RequestMapping("/hello")
public String sayHello() {
return "hello.html";
} }

然后就报错说会有个循环视图的错误,反正以后注意就是。

2.每次改完都要重新停止应用,再重新启动很烦~但springboot有个叫热部署的东西,就是说在项目中修改代码可以不用重新停止应用再重新启动,可以自动重启,这里我们用的是devtools:

具体见博客:https://www.cnblogs.com/cx-code/p/8686453.html

Springboot的static和templates的更多相关文章

  1. Springboot的static和templates区别

    static和templates部分参考博客:https://blog.csdn.net/wangb_java/article/details/71775637 热部署参考博客:https://www ...

  2. Django的学习——全局的static和templates的使用

    一.问题 首先我们在进行Django框架搭建的时候我们需要建立一个全局的变量,一是为了实现代码的复用,二是为了方便管理,如下图的样式 二.解决 1.修改setting里面的配置文件①templates ...

  3. springboot整合Thymeleaf模板引擎

    引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...

  4. django配置templates、static、media和连接mysql数据库

    1.模板文件 # =======templates配置======= if os.path.exists(os.path.join(BASE_DIR, 'templates')) is False: ...

  5. springboot 详细配置2

    # =================================================================== # COMMON SPRING BOOT PROPERTIE ...

  6. springboot + devtools(热部署)

    技术介绍 devtools:是boot的一个热部署工具,当我们修改了classpath下的文件(包括类文件.属性文件.页面等)时,会重新启动应用(由于其采用的双类加载器机制,这个启动会非常快,如果发现 ...

  7. (二)SpringBoot基础篇- 静态资源的访问及Thymeleaf模板引擎的使用

    一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf ...

  8. 补习系列(1)-springboot项目基础搭建课

    目录 前言 一.基础结构 二.添加代码 三.应用配置 四.日志配置 五.打包部署 小结 前言 springboot 最近火的不行,目前几乎已经是 spring 家族最耀眼的项目了.抛开微服务.技术社区 ...

  9. SpringBoot标准Properties

    # =================================================================== # COMMON SPRING BOOT PROPERTIE ...

随机推荐

  1. ffmpeg 中av_rescale_rnd 的含义

    http://blog.csdn.net/fireroll/article/details/8485482 一.函数声明: int64_t av_rescale_rnd(int64_t a, int6 ...

  2. mysql函数之七:replace() MySQL批量替换指定字段字符串

    mysql replace实例说明: UPDATE tb1 SET f1=REPLACE(f1, 'abc', 'def'); REPLACE(str,from_str,to_str) 在字符串 st ...

  3. hadoop各组件安装(非专业人士,不定期更新)

    压缩包下载http://www.cnblogs.com/bfmq/p/6027202.html 1.zookeepermkdir /usr/local/hadooptar zxf /root/zook ...

  4. git add . 的时候遇到warning: LF will be replaced by CRLF inXXX 解决办法

    $ git add . warning: LF will be replaced by CRLF in shop/Runtime/Cache/86bbc820c9ec1 d314a9c71cf5651 ...

  5. MSSQL 调用 .net 代码

    http://www.cnblogs.com/laozhao8/p/3398681.html 在SQL Server中调用.NET程序集   需求是这样的,我在.net程序里操作数据时将一些字段数据加 ...

  6. Flutter实战视频-移动电商-60.购物车_全选按钮的交互效果制作

    60.购物车_全选按钮的交互效果制作 主要做全选和复选框的这两个功能 provide/cart.dart 业务逻辑写到provide里面 先持久化取出来字符串,把字符串编程list.循环list ca ...

  7. 关于weblogic 10.3.6.0 的漏洞复现(1)

    最近小R 搭建了个weblogic,  因为之前在公司找系统漏洞的时候,发现了这个漏洞,所以为了特地专门搭建了个10.3.6.0版本. 漏洞编号: CVE-2017-10271 漏洞的描述:就是web ...

  8. E20190225-hm

    seal  n. 密封; 印章; 海豹; 封条;   v. 密封; 盖章; 决定; 封上(信封); primitive adj. 原始的; 发展水平低的; 落后的; [生物学] 原生的;  n. 原始 ...

  9. VS2010在WIN7下安装报错“下列组件安装失败”如何解决

    VS2010在WIN7下安装报错“下列组件安装失败”如何解决 http://www.111cn.net/net/42/75914.htm

  10. 自定义Mybatis返回类型及注意事项

    一.自定义返回拦截器package com.yaoex.crm.service.util; import org.apache.ibatis.session.ResultContext;import ...