spring-mvc - 使用 Spring MVC、Sitemesh、Freemarker 导入 spring.ftl

标签 spring-mvc freemarker sitemesh

如何使用 Spring MVC、Sitemesh 和 Freemarker 将 spring.ftl 宏导入 Freemarker 模板页面?

我已经基于 Ted Young's configuration example 使用 Sitemesh 和 Freemarker 配置了 Spring MVC 应用程序.根据Spring MVC/Freemarker集成引用,需要导入 spring.ftl 宏以便通过 <@spring.bind "command.name"/> 将支持模型绑定(bind)到 View 。但是,这样做:

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

导致此异常:
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 ,但我还没有在谷歌土地上找到解决方案。我也尝试使用 this technique (压缩 spring.ftl,将其放在 META-INF/lib 中,并将 zip 添加到构建路径),但似乎没有成功。

谢谢!

最佳答案

问题是 spring 不知道在哪里照顾 spring.ftl文件:
这是我使用 Boot 对 MVC 项目的自定义配置

/**
 * 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;
    }
}

前 2 个加载器允许加载 .ftl war 中的模板来自“/WEB-INF/ftl”和常规 jar 的文件来自 src/resources/templates 的文件.
如果您想在 freemarker 中使用安全标签,则重点是以下两行:
         viewResolver.setExposeSpringMacroHelpers(true);  
         viewResolver.setExposeRequestAttributes(true);

baseMvcTplLoader加载程序获取 spring.ftl来自 org.springframework.web.servlet.view.freemarker .我建议在一些 example project 中探索 ftl 模板或 documentation了解如何spring.ftl作品。

The configuration of the placeholder is not related to the freemarker configuration, yet its very useful for injecting values in variables from src/resources/application.properties by using the @Value annotation.



有了这个,您可以使用所有 Spring 内功率自由标记 模板。

关于spring-mvc - 使用 Spring MVC、Sitemesh、Freemarker 导入 spring.ftl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8450529/

相关文章:

Grails 2.4 ClassNotFoundException : Sitemesh GrailsPageFilter

java - tomcat中线程的文件问题

java.lang.NoClassDefFoundError : org/springframework/core/ResolvableTypeProvider- Spring Batch

spring - 如何根据需要标记 Spring MVC 参数

javascript - 显示带有时区的 freemarker 格式时间

grails - 网站网格作为grails UI框架是否响应?

java - Spring Rest Controller POST 请求不起作用

netsuite - 在 Netsuite 中,如何在项目履行生成的电子邮件中包含跟踪号码列表?

Java:将日期/时间选择器作为时间戳存储到mysql

spring-mvc - Sitemesh 不修饰返回的 View