spring boot 引入模板

今天主要说下,配置在resources文件中的内容怎样被spring boot所引用。

  • 引用静态模板的值
  • thymeleaf和spring boot的整合,及相关配置

根据springboot 推荐的格式,我们知道放在resources 都是一些资源,包括整个项目的配置啊 ,自定义配置啊  ,静态资源文件 (js,css等),以properties结尾。字面意思就是属性暂且就这么翻译吧。

application.properties是项目资源的配置,比如jar包的配置啊,比如默认端口是8080 但是我们想改成其他端口,怎么办了。只需要在这个系统配置文件里加上

server.port=端口号

当然,还有很多其他的方法,但是有这个简单吗。

下面来做个实例来演示如何把一个定义在静态资源文件的值映射到一个对象上;

  1. 新建一个com.xiaofeng.feng.pojo包,添加实体类Resource.java:

package com.xiaofeng.feng.pojo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ConfigurationProperties(prefix="com.xiaofeng.feng")
@PropertySource(value="classpath:response.properties")
public class Resource {
    private String name;
    private String website;
    private String language;

public String getName() {
        return this.name;
    }
    
    public String getWebsite(){
        return this.website;
    }
    
    public String getLanguage(){
        return this.language;
    }
    
    public void setName(String name) {
        this.name=name;
    }
    
    public void setWebsite(String website){
        this.website = website;
    }
    
    public void setLanguage(String language){
        this.language = language;
    }
}

比较简单,定义了三个属性:name,website,language;

  2.然后在resources下新建一个资源文件:resource.properties  定义如下:

  3.在HelloWorldController里面新添加一个类成员变量

	@Autowired
	private Resource resource;

在写一个方法看下返回结果:

    @RequestMapping("/getResource")
    public Object getResource(){
    	Resource bean=new Resource();
    	BeanUtils.copyProperties(resource, bean);

    	return bean;
    }

得到结果:

说明资源文件的内容已经成功映射到我们定义的类中了。

这个是为什么了,主要是

@Configuration
@ConfigurationProperties(prefix="com.xiaofeng.feng1")
@PropertySource(value="classpath:response.properties")

定义映射关系

Resource bean=new Resource();
BeanUtils.copyProperties(resource, bean);

绑定具体的值。

在这里顺带说下freemarker和thymeleaf   2种模板展示模板在spring boot中的整合。

thymeleaf

需要在pom.xml引入thymeleaf  包的引用

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

thymeleaf

在application.properties种配置如下

spring.thymeleaf.prefix=classpath:/templates/  /*模板的位置 这里是在resources/templates下*/
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.content-type=text/html
spring.thymeleaf.suffix=.html

新建一个ThymeleafController 实现如下:

	@RequestMapping("/index")
	public String index(ModelMap map){
		map.addAttribute("name", "thymeleaf xiaofeng");
		return "thymeleaf/index";
	}

在templates/thymeleaf下新建一个index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"></meta>
<title>Insert title here</title>
</head>
<body>
Thymeleaf 模板引起
<h1 th:text="${name}">hello world~~</h1>
</body>
</html>

注意标签对要写好 要不然会出错。

运行结果如下:

个人是比较推荐使用thymeleaf这个模板的,freemarker和它使用差不多,但是耦合比thymeleaf要高。

原文地址:https://www.cnblogs.com/lin-b/p/8620633.html

时间: 2024-07-30 18:05:17

spring boot 引入模板的相关文章

Spring Boot 整合模板引擎 Freemaker、thymeleaf

1.常用的 Spring Boot 2.x 模板引擎和官方推荐案例 1)JSP(后端渲染,消耗性能) Java Server Pages 动态网页技术,由应用服务器中的 JSP 引擎来编译和执行,再将生成的整个页面返回给客户端.优点是:可以写java代码.支持表达式语言(el.jstl).内建函数. 但 JSP 本质上是 Servlet,它非常占用 JVM 内存.Java Web 官方推荐,但 Spring Boot 不推荐https://docs.spring.io/spring-boot/d

spring boot Thymeleaf模板引擎 最简单输出例子

spring boot  Thymeleaf模板引擎  最简单输出例子 控制器代码如下: @GetMapping(value = "/test")public String test(Model model){ List<Boy> boy = new ArrayList<Boy>(); boy.add(new Boy("xx",11)); boy.add(new Boy("yy",22)); boy.add(new Boy

Spring Boot使用模板freemarker【从零开始学Spring Boot(转)

视频&交流平台: à SpringBoot网易云课堂视频 http://study.163.com/course/introduction.htm?courseId=1004329008 à Spring Boot交流平台 http://412887952-qq-com.iteye.com/blog/2321532 [原创文章,转载请注明出处] 103. Spring Boot Freemarker特别篇之contextPath[从零开始学Spring Boot] 最近有好久没有更新博客了,感谢

spring boot ----&gt; 常用模板freemarker和thymeleaf

===========================freemarker=================================== freemarker 官网:https://freemarker.apache.org/ freemarker starter: 1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starte

Spring Boot Thymeleaf 模板引擎的使用

Spring Boot 中可以支持很多模板引擎,Thymeleaf 是 Spring Boot 官方推荐使用的模板引擎,虽然在社区 Thymeleaf 的性能被许多人所吐糟,但这仍然不影响大量的开发人员使用他. Thymeleaf 是后台开发的最佳实践 当前 Spring Boot 2.0 及以后版本已经支持 Thymeleaf 3.0. 本章讲解如何在 Spring Boot 中使用 Themealf. 源码下载 欢迎关注我的微信公众号 程序鱼 ,我们一起编程聊天看世界. 1 创建一个 Spr

Spring Boot 引入org.springframework.boot.SpringApplication出错

今天新建的一个spring boot maven项目, 写启动类时发现无法引入SpringApplication, 经查原来是冲突了,我早些时候用了比较低版本的spring boot创建了项目 ,导致电脑用户文件夹下有旧版本的配置,现在用新的版本导致了冲突 ,那么只要删除冲突的老版本,文件夹地址 C:\Users\hanfuqingshi\.m2\repository\org\springframework\boot\spring-boot 下的2.0.0.RELEASE为新版本,而其它的为老版

Spring Boot 引入自定义yml

喜欢yml配置文件格式的人性化,也喜欢properties配置文件管理方式的人性化, 那么下面我们就来看一下 yml 是如何配置和使用类似properties管理方式的人性化. 配置文件 设置Spring Boot 系统 yml 和自定义 yml文件 application.yml spring: profiles: active: dev include: test #或者 include: "test" application: name: test-yml-application

Spring Boot引入某个包下部分Bean

Spring Boot环境下,假如有个第三方包third.jar,内有com.xxx.config目录,在com.xxx.config目录下有3个被@Component注释的类分别是A, B, C,现在我们需要在应用中让A注册到Spring容器中,而B,C不注册进来.有2个方法: 使用@ComponetScan的Filter,类似这样:@ComponentScan(basePackageClasses = A.class, useDefaultFilters = false, includeFi

Spring Boot引入Lombok

传统的写法,要写一串串的get().set()方法等等 现引入Lombok pom.xml引入 —————————————————————————————————————————— <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId></dependency> —————————————————————————————————