今天继续带来Demi的SpringBoot专题教程:静态化。
SpringBoot中默认使用thymeleaf模板引擎整合html。
SpringBoot不建议使用jsp。
html路径默认为
src/main/resources/templates/*.html,默认是从templates文件夹里找html资源。
1.pom.xml中配置thymeleaf的依赖:
<dependency>
<!--在pom.xml加入模板引擎thymeleaf的依赖:-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2. resources下创建templates文件夹,并新建tem.html:
注意:IDEA新建html,默认是不会补齐后面这个</meta>,我们必须手动补齐,否则页面会报错:
控制台会报错:
3. Controller:
@Controller
@RequestMapping("/tem")
public class TemController {
@RequestMapping("thymleaf")
public String hello(){
return "tem";
}
}
4. 打开浏览器,输入http://localhost:8080/tem/thymleaf
5. 更改默认路径:
(1)resources文件夹中新建application.properties:
########################################################
###THYMELEAF (ThymeleafAutoConfiguration)
###src/main/resources/templates/*.html 可改
########################################################
#路径
spring.thymeleaf.prefix=classpath:/tem/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
# ;charset=<encoding> is added
#spring.thymeleaf.content-type=text/html
# set to false for hot refresh
#spring.thymeleaf.cache=false
(2)重新运行:
1. pom.xml:
<!--在pom.xml加入模板引擎freemarker的依赖:-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2. *.ftl文件默认也是在
src/main/resources/templates/*.ftl中,前缀默认在templates文件夹中寻找资源,新建hello.ftl:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"></meta>
<title>tem</title>
</head>
<body>
<p>欢迎来到演示freemarker演示页面</p>
</body>
</html>
3. Controller:
@Controller
@RequestMapping("/templates")
public class TemController {
@RequestMapping("freemarker")
public String hello2(){
return "hello";
}
}
4. 打开浏览器,输入http://localhost:8080/templates/freemarker
5. 更改默认路径application.properties:
########################################################
###FREEMARKER (FreeMarkerAutoConfiguration)
###src/main/resources/templates/helloFtl.ftl 不可改
########################################################
#spring.freemarker.allow-request-override=false
#spring.freemarker.cache=false
#spring.freemarker.check-template-location=true
#spring.freemarker.charset=UTF-8
#spring.freemarker.content-type=text/html
#spring.freemarker.expose-request-attributes=false
#spring.freemarker.expose-session-attributes=false
#spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
#spring.freemarker.suffix=.ftl
#spring.freemarker.template-loader-path=classpath:/tem/ #comma-separated list
#spring.freemarker.view-names= # whitelist of view names that can be resolved
6. 存在的问题:
网上有资料说freemarker的访问路径是可以更改的,实际经过测试是不能更改的。
配置了spring.freemarker.prefix=classpath:/tem/ 就会报错。
1.创建maven web project,项目结构如图所示:
2. pom.xml:
<!-- servlet 依赖. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!-- JSTL-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
3.application.properties:
#定义返回jsp
# 返回jsp前缀
spring.mvc.view.prefix=/WEB-INF/jsp/
# 返回jsp后缀
spring.mvc.view.suffix=.jsp
#springboot默认端口号8080,更改:
#server.port=8081
#springboot默认访问地址是:http://ip:port/spring_boot,更改:
#server.context-path=/springboot
#server.address= # bind to a specific NIC
#server.session-timeout= # session timeout in seconds
#the context path, defaults to '/'
#server.servlet-path= # the servlet path, defaults to '/'
4.Controller:
@Controller
public class HelloController {
@RequestMapping("hello")
public String hello(Map<String,Object> map){
map.put("name","Demi");
return "index";
}
}
5. 项目结构:
6. 打开浏览器,输入http://localhost:8080/hello
访问成功。
源码:
github
SpringBoot使用thymleaf和freemarker:
https://github.com/southwind9801/springboot_01.git
SpringBoot整合JSP:
https://github.com/southwind9801/springboot_jsp.git