- 整合静态资源
- YML与Properties
- 渲染web页面
- 使用Freemarker模板引擎渲染web视图
- Freemarker配置
- 模板引擎条件判断用法
- 使用thymeleaf渲染web页面
- thymeleaf的使用
- 条件和循环的使用
整合静态资源
在我们开发Web应用的时候,需要引用大量的js、css、图片等静态资源。
Spring Boot默认提供静态资源目录需置于classpath下,目录需符合如下规则:
/static
/public
/resources
/META-INF/resources
举个例子,我们可以在src/main/resources/目录下创建static,在该位置放置一个图片文件。启动程序后,尝试访问http://localhost:8080/picture.jpg。如能显示图片,则配置成功。
SpringBoot支持两种配置方式,一种是properties文件,一种是yml。
配置文件的使用:
在resources目录下创建application.properties文件
文件中内容举例:
symc.name=forward symc.age=21
那么如何读取到这个文件呢?
定义属性,使用@Value1配置文件可以把配置文件的赋给对应的属性。
@SpringBootApplication
@RestController
public class ConfigPropertiesService {
@Value("${symc.name}")
private String name;
@Value("${symc.age}")
private Integer age;
@RequestMapping("/getProperties")
public String getProperties() {
return "name:" + name + "nage:" + age;
}
}
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(ConfigPropertiesService.class,args);
}
}
启动服务,访问
还可以使用yml的方式,过程都是一样的,就是配置文件不一样:
配置文件中的内容:
symc: name: Forward age: 21
很明显,properties相比yml比较冗余,所以推荐使用yml。
渲染web页面我们之前是通过@RestController来处理请求,返回的格式为json对象。那么如果需要渲染html页面的时候,要如何实现呢?
这时我们可以用到模板引擎 2,它能够非常好的帮助seo 3搜索到该网页。
在动态HTML实现上Spring Boot依然可以完美胜任,并且提供了多种模板引擎的默认配置支持,所以在推荐的模板引擎下,我们可以很快的上手开发动态网站。
Spring Boot提供了默认配置的模板引擎主要有以下几种:
- Thymeleaf
- FreeMarker
- Velocity
- Groovy
- Mustache
Spring Boot建议使用这些模板引擎,避免使用JSP,若一定要使用JSP将无法实现Spring Boot的多种特性。
当你使用上述模板引擎中的任何一个,它们默认的模板配置路径为:src/main/resources/templates。当然也可以修改这个路径,具体如何修改,可在后续各模板引擎的配置属性中查询并修改。
在pom.xml文件中引入依赖:
org.springframework.boot spring-boot-starter-freemarker 2.1.8.RELEASE
创建一个controller包,在resources目录下创建templates文件夹,现在我们的service包用于返回json对象,controller用于页面跳转
在controller下创建如下类FreemarkerIndexController :
这里不能使用@RestController了,因为他会返回一个json,应该用@Controller;
@Controller
public class FreemarkerIndexController {
@RequestMapping("/freemarkerIndex")
public String freemarkerIndex(Map result){
//转发到页面展示数据
//Map类似于HttpServletRequest中的request.setAttribute("name","Forward")
result.put("name","Forward");
return "freemarkerIndex";
}
}
在resources/templates下创建 * .ftl文件,里面放的是html代码:
${name}是请求响应的模板
Demo
${name}
要注意,我们的运行程序入口,需要扫包,必须在执行类同级或以下,还有run()中推荐写当前类:
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class);
}
}
Freemarker配置
我们可以在application.yml文件中配置,我们可以在这里设置编码;可以设置模板文件结尾,默认是 .ftl,我们可以修改成公司规定的后缀;我们还可以设置模板文件目录,默认是/templates,我们一样可以修改。
spring:
http:
encoding:
force: true
### 模版引擎编码为UTF-8
charset: UTF-8
freemarker:
allow-request-override: false
cache: false
check-template-location: true
charset: UTF-8
content-type: text/html; charset=utf-8
expose-request-attributes: false
expose-session-attributes: false
expose-spring-macro-helpers: false
## 模版文件结尾.ftl
suffix: .ftl
## 模版文件目录
template-loader-path: classpath:/templates
模板引擎条件判断用法
先整个例子:
@Controller
public class FreemarkerIndexController {
@RequestMapping("/freemarkerIndex")
public String freemarkerIndex(Map result){
//转发到页面展示数据
//Map类似于HttpServletRequest中的request.setAttribute("name","Forward")
result.put("name","Forward");
result.put("age",20);
result.put("sex","男");
ArrayList userList = new ArrayList();
userList.add("Forward");
userList.add("Gosling");
result.put("users",userList);
return "freemarkerIndex";
}
}
再到freemarkerIndex.ftl
${name}
<#--if-else的使用-->
<#--当遇到大于小于<>这样的,应使用(),不然会识别成标签-->
<#if (age>=18)>是个成年人
<#else >是未成年人
#if>
<#if sex=="男">是个男人
<#elseif sex=="女">是个女人
<#else >可能是个人妖
#if>
<#--list遍历-->
<#list users as user>
${user}
#list>


