Special Edition of the Spring Boot Freemarker contextPath learn from scratch [Spring Boot

 

Demand Origin: @ I was in the group: the main group Great God a question to ask, spring boot + freemarker how contextPath get a headache is killing me, not a fly online. I look at the previous blog

[Spring Boot using a template freemarker] seems really does not describe how to obtain contextPath in .ftl file, this is the article addresses the problem to be solved.

 

This chapter outlines:

(1) the issues raised;
how (2) spring is defined requestContextAttribute of;
how (3) Spring Boot should define it?
(4) Well there is a better solution?
(5) summary

 

 

       Next we together look at this section:

(1) the issues raised;

       Sometimes we need to get contextPath in freemarker template file .ftl, if no configuration parameters, then can not be obtained.

 

How (2) spring is defined in requestContextAttribute;

       It is used in the spring of a configuration file for the specified configuration, as follows:

<property name="requestContextAttribute" value="request"/>

       Once you've configured, we can use the following code in our x.ftl documents were introduced using:

${request.contextPath}。

 

How (3) Spring Boot should define it?

       In the spring it is to use the profile of the way, but we know that spring boot is basically zero configuration programming (although support mode profiles), then how should we do it? We can define a FreemarkerViewResolver the attribute value designating requestContextPath, specific code 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 2017 Nian 1 Yue 15 Ri

 */

@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 be used in $ {request.contextPath} x.ftl file.

 

(4) Well there is a better solution?

       Although the above way to solve the problem, but always felt around in a circle, we had to use freemarker when we use is used in the configuration file application.properties file, now back to the way to the introduction of the code, then can we next need to think about direct specified in application.properties it, the answer is yes. We only need to add the following configuration in application.properties in:

spring.freemarker.request-context-attribute=request

       Then you can use the $ ftl file in the {request.contextPath}.

 

(5) summary

       This article said that being said, it is actually very simple two-step process:

1, add the following information in application.properties:

spring.freemarker.request-context-attribute=request

2, carried out in x.ftl file:

${request.contextPath}

Guess you like

Origin www.cnblogs.com/kelelipeng/p/11433699.html