How to register attribute renderer in Apache Tiles?

两盒软妹~` 提交于 2019-12-11 03:37:26

问题


on the Integration with FreeMarker page on the Apache Tiles site it has:

To access ".ftl" files as attributes, register FreeMarkerAttributeRenderer this way (only available in a servlet environment):

@Override
protected void registerAttributeRenderers(
        BasicRendererFactory rendererFactory, TilesApplicationContext applicationContext,
        TilesRequestContextFactory contextFactory,
        TilesContainer container, AttributeEvaluator evaluator) {
    super.registerAttributeRenderers(rendererFactory, applicationContext, contextFactory,
            container, evaluator);
    FreeMarkerAttributeRenderer freemarkerRenderer = new FreeMarkerAttributeRenderer();
    freemarkerRenderer.setApplicationContext(applicationContext);
    freemarkerRenderer.setEvaluator(evaluator);
    freemarkerRenderer.setRequestContextFactory(contextFactory);
    freemarkerRenderer.setParameter("TemplatePath", "/");
    freemarkerRenderer.setParameter("NoCache", "true");
    freemarkerRenderer.setParameter("ContentType", "text/html");
    freemarkerRenderer.setParameter("template_update_delay", "0");
    freemarkerRenderer.setParameter("default_encoding", "ISO-8859-1");
    freemarkerRenderer.setParameter("number_format", "0.##########");
    freemarkerRenderer.commit();
    rendererFactory.registerRenderer("freemarker", freemarkerRenderer);
}

But I cannot figure out where I put this code. What is the normal spot you register this code on/which class do I extend and overwrite? Thanks for your help.


回答1:


hi,I figured out just now

this is my project herichy, I use java based config, and webconfig is just like web.xml

    @Bean
public TilesConfigurer tilesConfigurer() {
    TilesConfigurer tilesConfigurer = new TilesConfigurer();
    tilesConfigurer.setDefinitions("/WEB-INF/views/tiles/tiles.xml");
    tilesConfigurer.setCheckRefresh(true);
    tilesConfigurer.setTilesInitializer(new pringCompleteAutoloadTilesInitializer());
    return tilesConfigurer;
}

notice tilesConfigurer.setTilesInitializer(new pringCompleteAutoloadTilesInitializer());

this is SpringCompleteAutoloadTilesInitializer

public class SpringCompleteAutoloadTilesInitializer extends    CompleteAutoloadTilesInitializer
{
    @Override
    protected AbstractTilesContainerFactory   createContainerFactory(ApplicationContext context)
    {
        return new SpringCompleteAutoloadTilesContainerFactory();
    }
}

and that SpringCompleteAutoloadTilesContainerFactory is where you wanna put you code

you can check my project here git project




回答2:


This code goes into your TilesContainerFactory implementation.

(which usually extends BasicTilesContainerFactory).



来源:https://stackoverflow.com/questions/18771120/how-to-register-attribute-renderer-in-apache-tiles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!