SpringBoot编写html并热部署
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
SpringBoot编写html并热部署
1、下载htmleditor插件:eclipse-->help-->Eclipse MarketPlace-->HtmlEditor,下载完成后重启eclipse。
2、添加依赖及配置⽂件:
①热部署依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--devtools热部署 -->
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
②热部署配置(.properties⽂件):
spring.devtools.restart.enabled=true
③采⽤Thymeleaf模板引擎,因此需要添加Thymeleaf依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
④配置Thymeleaf要加载的⽂件(.properties⽂件):
spring.thymeleaf.prefix=classpath:/templates/
3、编写@Controller,以访问html⽂件:
package com.zr.ams.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class LoginController {
@GetMapping("login")
public String login12() {
return "index";
}
}
4、编写html⽂件(建议把脚本放在 <body> 元素的底部。
这会提⾼⽹页加载速度,因为 HTML 加载不受制于脚本加载。
):<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="/js/angular.min.js"></script>
</head>
<body>
<div ng-app="">
<p>名字 : <input type="text" ng-model="name"></p>
<h1>Hello {{name}}</h1>
</div>
</body>
</html>
5、springboot项⽬默认是不允许直接访问template下的⽂件的,是受保护的。
若想直接访问html⽂件,⽽不通过Controller层,则可通过以下配置完成(.properties⽂件):
spring.resources.static-locations=classpath:/templates/
6、templates下的html若⽆法访问static下的css,js类似,需做如下配置(.properties⽂件):
spring.resources.static-locations=classpath:/templates/,classpath:/static/。