1
votes

I am using Spring Batch Admin as a web frontend for my Spring Batch project together with Spring Boot.

Batch Admin provides some templates using Freemarker to set up the layout. I have added some more templates which are stored in src/main/webapp/web/layouts/html and the ressources are included in the packaging process into the .jar file.

When I start the app, my own layouts are not found ("layouts/html/myOwn.ftl not found" is the error message).

I can solve this by adding a FreeMarkerConfigurer like this:

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath"><value>classpath:/WEB-INF/</value></property>
 </bean>

However, when I do this, my own templates are found but the standard templates are gone (like layouts/html/home.ftl).

Is there a way to provide two paths or two template loaders such that the default template loader of Spring Batch Admin is not overwritten but used as a fallback?

Or is there any other solution like having the ressources in a specific place?

1
There's another property called templateLoaderPaths (note the "s" at the end), where you can specify multiple locations (example: stackoverflow.com/questions/36330590/…). I don't know where Spring Batch Admin loads the templates from though. - ddekany

1 Answers

0
votes

Thanks to @ddekany I came up with the following solution.

Necessary configuration for Freemarker:

<bean id="freemarkerConfig" class="org.springframework.batch.admin.web.freemarker.HippyFreeMarkerConfigurer">
    <property
        name="templateLoaderPaths"
        value="classpath:/WEB-INF/web,classpath:/org/springframework/batch/admin/web"
    />

    <property name="preferFileSystemAccess" value="false" />
    <property name="freemarkerVariables">
        <map>
            <entry key="menuManager" value-ref="menuManager" />
        </map>
    </property>
    <property name="freemarkerSettings">
        <props>
            <prop key="default_encoding">UTF-8</prop>
            <prop key="output_encoding">UTF-8</prop>
        </props>
    </property>
</bean>

The first property templateLoaderPaths (observe the additional s) allows to specify multiple paths separated by commas. The two paths are my own path classpath:/WEB-INF/web and the path to the default Spring Boot Admin files classpath:/org/springframework/batch/admin/web.

The additional configuration for the menuManager is necessary as otherwise the menu entries from the navigation disappear.

The custom freemarker layout files are stored in the default location src/main/webapp/WEB-INF/web/layouts/html/ and to be visible by the template loader have to be included in the jar build via

    <resources>
        <!-- copy the Freemarker templates -->
        <resource>
            <targetPath>WEB-INF</targetPath>
            <filtering>false</filtering>
            <directory>${basedir}/src/main/webapp/WEB-INF</directory>
        </resource>
    </resources>

in the project's pom.xml.