Spring Boot Freemarker special article contextPath [Learn Spring Boot from scratch]

How to get the contextPath in spring boot + freemarker is a headache for me, there is no reliable one on the Internet. I just looked at
[Spring Boot using template freemarker] in the previous blog. It seems that it does not introduce how to obtain the contextPath in the .ftl file. This is the problem to be solved in this article.

The outline of this chapter:

(1) The question is raised;
(2) How is the requestContextAttribute defined in spring;
(3) How should Spring Boot be defined?
(4) Is there a better solution?
(5) Summary


       Next, let's take a look at the content of this section:
(1) The question is raised;
       we sometimes need to obtain the contextPath in the freemarker template file .ftl. If some parameters are not configured, it cannot be obtained. .

(2) How is requestContextAttribute defined in spring;
       in spring, it is specified by using the configuration file method, as follows:
<property name="requestContextAttribute" value="request"/>
       After configuration, we can The following code is used in the x.ftl file to import and use:
${request.contextPath}.

(3) How should Spring Boot be defined?
       In spring, the configuration file is used, but we know that spring boot is basically zero-configuration programming (although it also supports the configuration file), so what should we do? We can define a FreemarkerViewResolver to specify the value of the requestContextPath property. The specific code is as follows:
package com.kfit.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework. web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
/**
*
* @author Angel -- Guardian Angel
* @version v.0.1
* @date January 15, 2017
*/
@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter{
    @Bean
    public FreeMarkerViewResolver freeMarkerViewResolver() {
        System.out.println("MvcConfig.freeMarkerViewResolver()");
        FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
        resolver.setPrefix("");
        resolver.setSuffix(" .ftl");
        resolver.setContentType("text/html; charset=UTF-8");
        resolver.setRequestContextAttribute("request");
        return resolver;
    }
}
After adding the above code, you can use it in the x.ftl file Use ${request.contextPath} too.

(4) Is there a better solution?



       以上方式虽然也能解决问题,但是总觉得绕了一个圈子,我们原本使用freemarker的时候,我们使用的是在配置文件application.properties文件进行使用的,现在又回到了代码的方式去引入了,那么要思考下我们可不可以在application.properties进行直接指定呢,答案是可以的。我们只需要在application.properties中添加如下配置:
spring.freemarker.request-context-attribute=request
       那么就可以在ftl文件中进行使用${request.contextPath}了。

(5)总结
       本文说了这么说,其实很简单就两个步骤:
1、在application.properties添加如下信息:
spring.freemarker.request-context-attribute=request
2、在x.ftl文件中进行使用:
${request.contextPath}


BTW:有时候我们为了代码的使用简单,request-context-attribute也会设置为ctx,那么使用的时候就是${ctx.contextPath}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326238171&siteId=291194637