Cześć,
Mam prosty projekt, gdzie korzystam z freemarkera ale tym razem konfiguruję wszystko za pomocą klas, odchodząc od .xmla. Natrafilem jednak na problem, otóż kiedy plik widoku wygląda tak:
<#import "spring.ftl" as spring />
<#--<@spring.bind "user"/>-->
<@spring.showErrors '*', 'errors' />
dostaję:
FreeMarker template error:
The following has evaluated to null or missing:
==> status [in template "spring.ftl" at line 331, column 12]
Kiedy usunę komentarz:
<#import "spring.ftl" as spring />
<@spring.bind "user"/>
<@spring.showErrors '*', 'errors' />
dostaję:
SEVERE: Error executing FreeMarker template
FreeMarker template error:
Java method "org.springframework.web.servlet.support.RequestContext.getBindStatus(String)" threw an exception when invoked on org.springframework.web.servlet.support.RequestContext object "org.springframework.web.servlet.support.RequestContext@6ebca4c0"; see cause exception in the Java stack trace.
Klasa konfiguracyjna:
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
import freemarker.template.TemplateException;
import freemarker.template.utility.XmlEscape;
@Configuration
@EnableWebMvc
@ComponentScan({ "info.noteme" })
@ContextConfiguration(classes = { WebConfig.class })
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public FreeMarkerViewResolver freemarkerViewResolver() {
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
resolver.setOrder(1);
resolver.setCache(false);
resolver.setPrefix("");
resolver.setSuffix(".ftl");
return resolver;
}
@Autowired
private XmlEscape fmXmlEscape;
@Bean
public XmlEscape fmXmlEscape() {
return new XmlEscape();
}
@Bean
public FreeMarkerConfigurer freemarkerConfig(WebApplicationContext applicationContext) throws IOException, TemplateException {
FreeMarkerConfigurer config = new FreeMarkerConfigurer();
Map<String, Object> variables = new HashMap<String, Object>();
Properties property = new Properties();
property.setProperty("default_encoding", "UTF-8");
variables.put("xml_escape", fmXmlEscape);
config.setServletContext(applicationContext.getServletContext());
config.setFreemarkerVariables(variables);
config.setTemplateLoaderPath("/WEB-INF/views/ftl");
config.setFreemarkerSettings(property);
return config;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
Ktoś kojarzy co tutaj może nie grać?
edit: naprawione, zabrakło @ModelAttribute
w definicji kontrolera, który wywoływał widok