2011-12-09 13 views
5

Jak importować makra spring.ftl do strony szablonu Freemarker przy użyciu Spring MVC, Sitemesh i Freemarker?Importowanie pliku spring.ftl przy użyciu Spring MVC, Sitemesh, Freemarker

Skonfigurowałem aplikację Spring MVC za pomocą Sitemesh i Freemarker na podstawie Ted Young's configuration example. Zgodnie z odniesieniem do integracji z Spring MVC/Freemarker konieczne jest zaimportowanie makr spring.ftl w celu związania modelu podkładu z widokiem za pomocą < @ spring.bind "command.name" />. Jednak to robi:

<#import "/spring.ftl" as spring> 
<@spring.bind "command.user"/> 

Wyniki w tym wyjątkiem:

org.springframework.web.util.NestedServletException: 
Request processing failed; nested exception is freemarker. 
template.TemplateException: Error reading imported file spring.ftl 

Others have experienced this issue, ale mam jeszcze do znalezienia rozwiązania w google ziemi. Próbowałem również użyć this technique (zip up spring.ftl, umieszczając go w META-INF/lib i dodając zip do ścieżki budowania), ale nie wydaje się, że to działa.

Dzięki!

Odpowiedz

-2
<#import "spring.ftl" as spring/> 

Bez /

+0

Naprawdę? [Dokumentacja Springa] (http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/view.html) mówi o użyciu '<#import" /spring.ftl " jako wiosna /> ​​' – Raedwald

1

Problem polega na tym, że sprężyna nie wiem gdzie szukać po pliku spring.ftl: To jest moja konfiguracja niestandardowych dla projektu MVC przy użyciu Boot

/** 
* Otras configuraciones de la aplicaciones web, incluyendo algunas definidas en 
* xml. Usar @ImportResource("classpath:/extra-config.xml") en caso de quererse 
* importar configuracion en xml 
*/ 
@Configuration 
@PropertySource("classpath:application.properties") 
public class WebAppConfig 
{ 
    @Autowired 
    private ServletContext context; 

    @Bean 
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { 
     PropertySourcesPlaceholderConfigurer placeHolderConfigurer = new PropertySourcesPlaceholderConfigurer(); 
     return placeHolderConfigurer; 
    } 

    @Bean 
    public FreeMarkerConfigurer freeMarkerConfigurer() throws IOException, TemplateException 
    { 
     FreeMarkerConfigurer configurer = new FreeMarkerConfigurer() 
     { 

      @Override 
      protected void postProcessConfiguration(freemarker.template.Configuration config) throws IOException, TemplateException 
      { 
       WebappTemplateLoader WebAppTplLoader = new WebappTemplateLoader(context, "/WEB-INF/ftl"); 
       ClassTemplateLoader classTplLoader = new ClassTemplateLoader(context.getClassLoader(), "/templates"); 
       ClassTemplateLoader baseMvcTplLoader = new ClassTemplateLoader(FreeMarkerConfigurer.class, ""); 
       MultiTemplateLoader mtl = new MultiTemplateLoader(new TemplateLoader[] 
       { 
        WebAppTplLoader, 
        classTplLoader, 
        baseMvcTplLoader 
       }); 
       config.setTemplateLoader(mtl); 
      } 
     }; 
     configurer.setDefaultEncoding("UTF-8"); 
     configurer.setPreferFileSystemAccess(false); 
     return configurer; 
    } 

    @Bean 
    public FreeMarkerViewResolver viewResolver() 
    { 
     FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver(); 
     viewResolver.setExposeSpringMacroHelpers(true); 
     viewResolver.setExposeRequestAttributes(true); 
     viewResolver.setPrefix(""); 
     viewResolver.setSuffix(".ftl"); 
     viewResolver.setContentType("text/html;charset=UTF-8"); 
     return viewResolver; 
    } 
} 

Pierwsze 2 Ładowarki pozwalają załadować szablony .ftl w plikach war z "/ WEB-INF/ftl" oraz ze zwykłych plików jar z src/resources/templates. Jeśli chcesz używać znaczników w zabezpieczeniach freemarker na escense są to dwie linie:

  viewResolver.setExposeSpringMacroHelpers(true); 
     viewResolver.setExposeRequestAttributes(true); 

a ładowarka uzyskać spring.ftl z org.springframework.web.servlet.view.freemarkerbaseMvcTplLoader. Radzę zbadać szablony ftl w niektórych example project lub documentation, aby mieć wskazówkę, jak działa spring.ftl.

Konfiguracja zastępczy nie jest związana z konfiguracją freemarker , jednak jego bardzo przydatne do wstrzykiwania wartości zmiennych z src/resources/application.properties stosując @Value opisów.

Z tego można wykorzystać wszystkie sprężyny siłę ciągu freemarker szablonów.

0

Podoba mi się mój spring.ftl uwzględniony domyślnie bez konieczności dodawania go ręcznie w każdym widoku. W twojej konfiguracji.

Zdefiniuj swojego FreemarkerConfigurer jako takiego.

@Bean(name = "freemarkerConfig") 
public FreeMarkerConfigurer freemarkerConfig() { 
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); 
    configurer.setTemplateLoaderPath("/WEB-INF/views/"); 
    Map<String, Object> map = new HashMap<>(); 
    map.put("xml_escape", new XmlEscape()); 
    configurer.setFreemarkerVariables(map) 
    def settings = new Properties() 
    settings['auto_import'] = 'spring.ftl as spring, layout/application.ftl as l' 
    configurer.setFreemarkerSettings(settings) 
    println "returning freemarker config" 
    return configurer; 
} 
Powiązane problemy