Search code examples
springspring-batchcustomizationfreemarkerspring-batch-admin

Spring Batch Admin Customization Issues with Freemarker Not Picking Up Model Attributes


I am currently trying to extend Spring Batch Admin for an enterprise project. I have the basic SBA setup integrated into my project already but I was looking to add some new features. The feature I am trying to add is one I found online here. One thing I have encountered so far is that when trying to add a new Controller and ftl template, the model has issues binding the Controller/Model to the view.

What I was hoping you could help me with is what additional configurations are needed to extend SBA (relatively step-by-step). I am not new to Spring or Spring Batch but I am very new to Freemarker. There seems to be some confusion about how Freemarker picks up attributes from my Controller when passed to the model.

So far I have the ftl template I want to use (which picks up from the standard template for SBA). This is basically a modified version of one of the default ftl files found in spring-batch-admin-manager.jar:

<#import "/spring.ftl" as spring />
<@spring.bind "model"/>
<div id="job-triggers">

<h2>${MODEL_ATTRIBUTE_SEARCH_RESULT_MESSAGE}</h2>

<p/>

<#if fireTimes??>
    <table title="Future Job Executions"
           class="bordered-table">
        <tr>
            <th>Job Name</th>
            <th>Date</th>
        </tr>
        <#list fireTimes as execution>
            <#if execution_index % 2 == 0>
                <#assign rowClass="name-sublevel1-even" />
            <#else>
                <#assign rowClass="name-sublevel1-odd" />
            </#if>
            <tr class="${rowClass}">
                <#assign execution_url><@spring.url relativeUrl="${servletPath}/jobs/${execution.jobName?c}"/></#assign>
                <td>${execution.jobName}</td>
                <td>${execution.date}</td>
            </tr>
        </#list>
    </table>
    <#if startJobExecution??>
        <ul class="controlLinks">
            <#assign executions_url><@spring.url relativeUrl="${servletPath}/jobs/executions"/></#assign>
            <li>Rows: ${startJobExecution}-${endJobExecution} of ${totalJobExecutions}</li>
            <#if nextJobExecution??><li><a href="${executions_url}?startJobExecution=${nextJobExecution?c}&pageSize=${pageSize!30}">Next</a></li></#if>
            <#if previousJobExecution??><li><a href="${executions_url}?startJobExecution=${previousJobExecution?c}&pageSize=${pageSize!30}">Previous</a></li></#if>
            <!-- TODO: enable pageSize editing -->
            <li>Page Size: ${pageSize!30}</li>
        </ul>
    </#if>
</#if>

Then I have a Controller which has a ModelMap parameter to the GET request to which I put different attributes with model.addAttribute("someName", someObject):

@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(@ModelAttribute("model") ModelMap model) throws SchedulerException, ParseException {

    Scheduler scheduler = appCtx.getBean(SchedulerFactoryBean.class).getObject();
    Calendar cal = Calendar.getInstance();
    Date from = cal.getTime();
    cal.add(Calendar.DATE, 1);
    Date to = cal.getTime();

    List<FireTime> fireTimes = getFiretimesForTriggers(scheduler, from, to);
    Collections.sort(fireTimes);
    model.addAttribute("fireTimes", fireTimes);

    if(fireTimes.isEmpty()) {
        model.addAttribute(MODEL_ATTRIBUTE_SEARCH_RESULT_MESSAGE,"No firetimes found between " + DATE_FORMAT.format(from) + " and " + DATE_FORMAT.format(to));
    } else {
        model.addAttribute(MODEL_ATTRIBUTE_SEARCH_RESULT_MESSAGE, fireTimes.size() + " firetimes found between" + DATE_FORMAT.format(from) + " and " + DATE_FORMAT.format(to));
    }
    return INDEX_VIEW;
}

Other than these couple of files, I don't really have any additional configuration except for the definition of the new view in my servlet-config.xml:

<import resource="classpath:/applicationContext.xml"/>
<import resource="classpath*:/META-INF/spring/batch/servlet/resources/*.xml" />
<import resource="classpath*:/META-INF/spring/batch/servlet/manager/*.xml" />

<context:component-scan base-package="com.basepackage.where.my.controller.components.are" />

<bean name="firetimesView" parent="standard">
    <property name="attributes">
        <props merge="true">
            <prop key="body">/firetimesView.ftl</prop>
            <prop key="titleCode">fire.times.title</prop>
            <prop key="titleText">Fire Times</prop>
        </props>
    </property>
</bean>

The issue I am having is that Freemarker gives errors that the referenced model components are invalid references:

freemarker.core.InvalidReferenceException: Expression MODEL_ATTRIBUTE_SEARCH_RESULT_MESSAGE is undefined on line 5, column 11 in firetimesView.ftl.
freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124)
freemarker.core.Expression.getStringValue(Expression.java:118)
freemarker.core.Expression.getStringValue(Expression.java:93)
freemarker.core.DollarVariable.accept(DollarVariable.java:76)
freemarker.core.Environment.visit(Environment.java:209)
freemarker.core.MixedContent.accept(MixedContent.java:92)
freemarker.core.Environment.visit(Environment.java:209)
freemarker.core.Environment.include(Environment.java:1482)
freemarker.core.Include.accept(Include.java:169)
freemarker.core.Environment.visit(Environment.java:209)
freemarker.core.MixedContent.accept(MixedContent.java:92)
freemarker.core.Environment.visit(Environment.java:209)
freemarker.core.Environment.process(Environment.java:189)
freemarker.template.Template.process(Template.java:237)
org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:366)
org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:283)
org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:233)
org.springframework.batch.admin.web.freemarker.AjaxFreeMarkerView.renderMergedTemplateModel(AjaxFreeMarkerView.java:107)
org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:167)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:263)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.web.filter.ShallowEtagHeaderFilter.doFilterInternal(ShallowEtagHeaderFilter.java:73)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

I also have the necessary getters/setters needed for setter injection and accessing the variables from the view. Any ideas as to why Freemarker can't pick up the attributes I am adding to the map? Any help would be appreciated!


Solution

  • Since you are using ModelMap you could use put instead of addAttribute. Finally, your code should look like this :

     if(fireTimes.isEmpty()) {
            model.put(MODEL_ATTRIBUTE_SEARCH_RESULT_MESSAGE,"No firetimes found between " + DATE_FORMAT.format(from) + " and " + DATE_FORMAT.format(to));
        } else {
            model.put(MODEL_ATTRIBUTE_SEARCH_RESULT_MESSAGE, fireTimes.size() + " firetimes found between" + DATE_FORMAT.format(from) + " and " + DATE_FORMAT.format(to));
        }
    

    If that doesn't work, just try making it a Model instead of ModelMap.