春天mvc多个模板解析器(thymeleaf)

时间:2015-10-30 08:30:30

标签: java spring-mvc thymeleaf

简而言之,我想要实现的是为我拥有的每个项目加载多个视图解析器。我们如何以编程方式添加视图解析器?

我的结构如下:
    | __家长
    | ______ ACC1
    | ______ ACC2
    | ______ ACC3
    | ______ ...

我的代码:

@Configuration
@ComponentScan(basePackages = {"com.compname.services"})
public class YPIPortletConfig implements BeanDefinitionRegistryPostProcessor {

/**
 * Logger 
 */

private BeanReference createResolver(String beanName, File file, int order, BeanDefinitionRegistry registry) throws BeansException { 
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(FileTemplateResolver.class).setLazyInit(true); 
    // Utilise le « file » en tant que propriété de ta bean

    builder.addPropertyValue("cacheable", false);
    builder.addPropertyValue("templateMode", "HTML5");
    builder.addPropertyValue("order", order);
    builder.addPropertyValue("prefix", file.getAbsolutePath());


    AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
    registry.registerBeanDefinition(beanName, beanDefinition);
    return new RuntimeBeanNameReference(beanName);
}


private BeanReference createTemplateEngine(BeanDefinitionRegistry registry) throws BeansException {
    String beanName = "templateEngine1";
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(SpringTemplateEngine.class).setLazyInit(true);

    // Utilise le builder pour ajouter ta liste en tant que propriété
    // builder.addPropertyValue("templateResolvers", set);

    Set<BeanReference> set = new LinkedHashSet<BeanReference>();

    set.add(new RuntimeBeanNameReference("templateResolver1"));
    set.add(new RuntimeBeanNameReference("templateResolver2"));
    set.add(new RuntimeBeanNameReference("templateResolver3"));
    set.add(new RuntimeBeanNameReference("templateResolver4"));

    builder.addPropertyValue("templateResolvers", set);

    AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
    registry.registerBeanDefinition(beanName, beanDefinition);
    return new RuntimeBeanNameReference(beanName);
}

public BeanReference createThymeleafViewResolver(BeanDefinitionRegistry registry) {
    String beanName = "viewResolver";
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ThymeleafViewResolver.class).setLazyInit(true);
    builder.addPropertyValue("characterEncoding", "UTF-8");
    // builder.addPropertyValue("templateEngine", createTemplateEngine(registry));
    builder.addPropertyReference("templateEngine", "templateEngine1");

    AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
    registry.registerBeanDefinition(beanName, beanDefinition);
    return new RuntimeBeanNameReference(beanName);
}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}

@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
    createResolver("templateResolver1", new File ("C:/liferay/myFolder/acc_1/resources"), 0, registry);
    createResolver("templateResolver2", new File ("C:/liferay/myFolder/acc_2/resources"), 1, registry);
    createResolver("templateResolver3", new File ("C:/liferay/myFolder/acc_3/resources"), 2, registry);
    createResolver("templateResolver4", new File ("C:/liferay/myFolder/vir_4/resources"), 3, registry);
    createTemplateEngine(registry);
    createThymeleafViewResolver(registry);
}
}

如果我尝试加载一条路径,一切都很好。

我必须这样做java风格而不是xml风格,因为我需要循环在外部文件中定义的动态数量的文件夹。

我遇到的例外情况如下:

 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'viewResolver': Cannot resolve reference to bean 'templateEngine1' while setting bean property 'templateEngine'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'templateEngine1': Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.LinkedHashSet' to required type 'java.util.Set' for property 'templateResolvers'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.beans.factory.config.RuntimeBeanNameReference] to required type [org.thymeleaf.templateresolver.ITemplateResolver] for property 'templateResolvers[0]': no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1469)  

由于我是两个框架(sp&amp; th)的新手,因此非常感谢任何深入的解释(如果可能的话)。

谢谢。

1 个答案:

答案 0 :(得分:2)

解决方案是使用 ManagedSet ,而不是仅使用设置

ManagedSet templateResolversSet = new ManagedSet();
…
builder.addPropertyValue("templateResolvers", templateResolversSet);