模板引擎(thymeleafAPI)

  1. 与springboot整合maven配置
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    
  2. thymeleaf的简单配置及使用
    (1) 添加maven依赖
    (2) 在resources/templates目录下添加模板
    (3) 编写Controller
  3. 配置文件配置信息
    #启用模板缓存。
    spring.thymeleaf.cache = true  
    #在呈现模板之前检查模板是否存在。
    spring.thymeleaf.check-template = true 
    #检查模板位置是否存在。
    spring.thymeleaf.check-template-location = true 
    #Content-Type值。
    spring.thymeleaf.content-type = text/html 
    #启用MVC Thymeleaf视图分辨率。
    spring.thymeleaf.enabled = true  
    #模板编码。
    spring.thymeleaf.encoding = utf-8
    #应用于模板的模板模式,默认HTML5。另请参见StandardTemplateModeHandlers。
    spring.thymeleaf.mode = HTML
    #在构建URL时预先查看名称的前缀,模板路径。
    spring.thymeleaf.prefix = classpath:/templates/
    #构建URL时附加到查看名称的后缀,模板后缀名。
    spring.thymeleaf.suffix = .html  
    #链中模板解析器的顺序。
    #spring.thymeleaf.template-resolver-order =
    #可以解析的视图名称的逗号分隔列表。
    #spring.thymeleaf.view-names =
    #应该从解决方案中排除的视图名称的逗号分隔列表。
    #spring.thymeleaf.excluded-view-names =
    
  4. 国际化配置
    (1) 新建不同语言的配置文件
    home.properties 默认配置文件
    home_en_US.properties 英文配置文件
    home_zh_CN.properties 中文配置文件
    (2) 新家配置,指定语言配置文件目录
    properties # i18n为resources下目录,home为语言配置文件前缀 spring.messages.basename=i18n/home
    (3) 新增区域解析器
    package com.zp.demo;
    import org.springframework.web.servlet.LocaleResolver;
    import org.thymeleaf.util.StringUtils;   
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.util.Locale;   
    /**
    * 可以在连接上携带区域信息
    */
    public class MyLocaleResolver implements LocaleResolver {
       @Override
       public Locale resolveLocale(HttpServletRequest request) {
           //获取连接路径上的l后的值 l=zh_CN or en_US
           String l = request.getParameter("l");
           //如果没有值则使用默认语言版本
           Locale locale = Locale.getDefault();
           //如果有值则进行语言切换
           if(!StringUtils.isEmpty(l)){
               String[] split = l.split("_");
               locale = new Locale(split[0],split[1]);
           }
           return locale;
       }
       @Override
       public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
    
       }
    }
    
    package com.zp.demo;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.LocaleResolver;
    @Configuration
    public class MyAppConfig  {
        @Bean
        public LocaleResolver localeResolver() {
            return new MyLocaleResolver();
        }
    }
    
    (4) 页面添加语言切换按钮
     /index为页面跳转接口,l与解析器中获取的参数一致
     <a th:href="@{/index?(l='zh_CN')}">中文</a>
     <a th:href="@{/index?(l='en_US')}">English</a>
    
    (5) 需要国际化的页面需从配置文件中获取数据
     #{}引入的变量会根据区域设置读取不同的文件
     <div th:text="#{home.name}"></div>
    
    (6) #{}引入可加参数
     页面引用,可设置不可参数
     <div th:text="#{home.name('参数1','参数1')}"></div>2
     home.properties配置文件配置,可在信息中通过{index}引入参数,index为参数位置索引
     home.name = 参数1{0}和{1}
    
    (7) 消息key及参数都可使用变量
     <div th:utext="#{${homeMsgKey}(${session.user.name})}"></div>
    
  5. 设置全局变量
    package com.zp.demo;
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import org.thymeleaf.spring5.view.ThymeleafViewResolver;
    import javax.annotation.Resource;
    import java.util.HashMap;
    import java.util.Map; 
    /**
     * 重写configureViewResolvers方法,设置StaticVariables即可
     */
    @Component
    public class WebMvcConfigurerAdapter implements WebMvcConfigurer {
        @Resource(name = "thymeleafViewResolver")
        private ThymeleafViewResolver thymeleafViewResolver;   
        @Override
        public void configureViewResolvers(ViewResolverRegistry registry) {
            if (thymeleafViewResolver != null) {
                Map<String, Object> vars = new HashMap<>(1);
                vars.put("version", "1.0.0");
                thymeleafViewResolver.setStaticVariables(vars);
            }
            WebMvcConfigurer.super.configureViewResolvers(registry);
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郑重其事,鹏程万里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值