Question

I am trying to get request parameters on freemarker page. But I can't figure out how to setup implicit variables like RequestParameters or so.

Here is a thread http://forum.springsource.org/showthread.php?t=32846 but I don't where he setup that RequestParameters object.

The only thing I found in docs (more in source code), that it is FreemarkerServlet which can place that variable. But I don't have it in my application. All configurations are done via Spring, i.e. View Resolver.

Can anybody help me?

Was it helpful?

Solution

For query string ?myparam=abc, you can get to myparam like that:

${RequestParameters.myparam}

Verified on Spring 3 + Freemarker 2.3.

Don't need any additional configuration for the view handler.

OTHER TIPS

Make sure that exposeRequestAttributes is enabled in your FreeMarkerViewResolver, e.g.

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
      <property name="cache"  value="true" />
      <property name="prefix" value="" />
      <property name="suffix" value=".ftl" />
      <property name="requestContextAttribute"  value="request" />
      <property name="exposeSpringMacroHelpers" value="true" />
      <property name="exposeRequestAttributes"  value="true" />
      <property name="exposeSessionAttributes"  value="true" />

Spring boot 1.5 based in anotation into class will be something like:

package com.openkm.config;

import java.io.IOException;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import freemarker.cache.TemplateLoader;
import freemarker.template.TemplateException;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/").setCachePeriod(604800); // One week
    }

    @Bean(name = "freeMarkerViewResolver")
    public FreeMarkerViewResolver getFreeMarkerViewResolver() {
        FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
        viewResolver.setExposeSpringMacroHelpers(true);  
        viewResolver.setExposeRequestAttributes(true);
        viewResolver.setExposeSessionAttributes(true);
        viewResolver.setPrefix("/WEB-INF/ftl/");
        viewResolver.setSuffix(".ftl");
        viewResolver.setOrder(0);
        viewResolver.setContentType("text/html; charset=UTF-8");
        viewResolver.setCache(false);
        viewResolver.setRequestContextAttribute("request");
        return viewResolver;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top