contextPath in Spring Boot Freemarker Special

[Spring Boot uses the template freemarker] It seems that it does not introduce how to obtain the contextPath in the .ftl file, which 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 --守护天使
* @version v.0.1
* @date 2017年1月15日
*/
@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 ${request.contextPath} in the x.ftl file.

(4) Is there a better solution?
Although the above method can also solve the problem, it always feels like a circle. When we originally used freemarker, we used it in the configuration file application.properties file, and now we have returned to the code method to introduce it, then To think about whether we can directly specify it in application.properties, the answer is yes. We just need to add the following configuration in application.properties:
spring.freemarker.request-context-attribute=request
Then you can use ${request.contextPath} in the ftl file.

(5) Summary
This article said so, in fact, it is very simple in two steps:
1. Add the following information in application.properties:
spring.freemarker.request-context-attribute=request
2. Use it in the x.ftl file:
${request.contextPath}


BTW: Sometimes we set the request-context-attribute to ctx for the simplicity of the code, so when using it, it is ${ctx.contextPath}

Guess you like

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