从外部服务器渲染Thymeleaf中的html片段

时间:2019-04-29 06:12:18

标签: java html spring spring-boot thymeleaf

我在名为home.html的Spring Boot项目中有主页。我有一个页面控制器来渲染此home.html。下面是控制器的代码。

@RequestMapping(value = "/home")
public String goToHome(Model model) {
    return "home";
}

在我的项目中,home.htmlwebapp/WEB-INF/static/文件夹中。因此路径为webapp/WEB-INF/static/home.html

我的home.html如下所示

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/html">
<head>
     <title>home</title>
</head>
<body>
    <object th:include="/en/home :: home-body" th:remove="tag"/>
</body>
</html>

我在外部服务器中具有home.html文件,如下所示。

<div th:fragment="home-body">
    <p>Welcome to home page</p>
</div>

此文件的路径为http://external-server.com/fragments/en/home.html

我想要做的是渲染home-body片段部分,该片段部分是从驻留在Spring Boot Tomcat服务器中的home.html内部的外部服务器中获取的。

我添加了以下Bean作为配置,以获取此home-body片段。下面是该代码。

@Bean
    public UrlTemplateResolver urlTemplateResolver(){
        UrlTemplateResolver urlTemplateResolver = new UrlTemplateResolver();
        boolean isCmsContentEnable = Boolean.parseBoolean( env.getProperty( ConfigurationConstants.CMS_CONTENT_ENABLE ) );
        if( isCmsContentEnable )
        {
            urlTemplateResolver.setPrefix( "http://external-server.com/fragments/" );
            urlTemplateResolver.setSuffix( ".html" );
            urlTemplateResolver.setCharacterEncoding( "UTF-8" );
            urlTemplateResolver.setTemplateMode( "HTML5" );
            urlTemplateResolver.setCacheable( false );
            urlTemplateResolver.setOrder(10);
        }
        return urlTemplateResolver;
    }

现在,当我尝试转到主页时,发生的是代替呈现home-body片段(驻留在spring boot tomcat服务器中的home.html内部),而是直接呈现home.html驻留在外部服务器中。

如何解决此问题?谁能指出我在这里做错了什么。谢谢

1 个答案:

答案 0 :(得分:0)

使用SpringTemplateEnginethymeleafTemplateResolver作为templateResolvers创建urlTemplateResolver

   @Bean public ThymeleafViewResolver thymeleafViewResolver()
{
    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setTemplateEngine( templateEngine() );
    return viewResolver;
}

@Bean public SpringTemplateEngine templateEngine()
{
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    Set<ITemplateResolver> templateResolvers = new HashSet();
    templateResolvers.add( thymeleafTemplateResolver() );
    templateResolvers.add( urlTemplateResolver() );
    templateEngine.setTemplateResolvers( templateResolvers );

    return templateEngine;
}

@Bean public SpringResourceTemplateResolver thymeleafTemplateResolver()
{
    SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
    templateResolver.setPrefix( "/WEB-INF/static/" );
    templateResolver.setSuffix( ".html" );
    templateResolver.setTemplateMode( "HTML5" );
    templateResolver.setOrder( 2 );
    return templateResolver;
}

@Bean public UrlTemplateResolver urlTemplateResolver()
{
    UrlTemplateResolver urlTemplateResolver = new UrlTemplateResolver();
    urlTemplateResolver.setCharacterEncoding( "UTF-8" );
    urlTemplateResolver.setTemplateMode( "HTML5" );
    urlTemplateResolver.setCacheable( false );
    urlTemplateResolver.setOrder( 3 );
    return urlTemplateResolver;
}

然后像这样访问外部模板。

 <div th:include="http://banner.server.com/myBanner.html :: fragmentName">