2

I'm having some trouble trying to have both a simple static plain html and a ftl with freemarker working at the same time.

I have Spring 4.0.2.RELEASE and the following web configuration

@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy
@ComponentScan({"com.test.myproject"})
@ImportResource("classpath:application-context.xml")
public class TrackerWebConfig
    extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(this.jacksonConverter());
    }

    @Bean
    public MappingJackson2HttpMessageConverter jacksonConverter() {
        return new MappingJackson2HttpMessageConverter();
    }

    @Bean
    public ExecutorService upaClientMonitorThreadPool() {
        return Executors.newScheduledThreadPool(1);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        super.addResourceHandlers(registry);
        registry.addResourceHandler("/static/html/**").addResourceLocations("classpath:/static/html/");
        registry.addResourceHandler("/static/css/**").addResourceLocations("classpath:/static/css/",
            "classpath:/com/despegar/api/documentation/views/static/css/");
        registry.addResourceHandler("/static/js/**").addResourceLocations("classpath:/static/js/",
            "classpath:/com/despegar/api/documentation/views/static/js/");
        registry.addResourceHandler("/static/img/**").addResourceLocations("classpath:/static/img/",
            "classpath:/com/despegar/api/documentation/static/img/");
        registry.addResourceHandler("/static/fonts/**").addResourceLocations("classpath:/static/fonts/");
    }
}

I had to import legacy configuration which included freemarker on a xml

<bean id="freemarkerConfig"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="preferFileSystemAccess" value="false" />
        <property name="templateLoaderPaths">
            <list>
                <value>classpath:com/despegar/api/documentation/views/</value>
            </list>
        </property>
        <property name="freemarkerSettings">
            <props>
                <prop key="template_exception_handler">html_debug</prop>
                <prop key="output_encoding">UTF-8</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="cache" value="false" />
        <property name="prefix" value="" />
        <property name="suffix" value=".ftl" />
        <property name="exposeSpringMacroHelpers" value="true" />
        <property name="order" value="1" />

And a simple controller

@Controller
@RequestMapping("/test")
public class ExampleHtmlController {

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/static/html/index.html";
    }

}

This controller stopped working when I added the xml configuration. If I comment the FreeMarkerViewResolver in the xml, it starts working again.

From what I understand, the view resolver is getting in the middle of the ResourceHandlerRegistry and the request, returning a Could not resolve view with name '/static/html/index.html' in servlet with name 'dispatcher'

Is there any way to make the static html and the freemarkerviewresolver to coexist?

1 Answers1

0

I would try with <mvc:resources mapping="/your_static_resources_mapping/**" location="/your_static_resources_folder/"/> directive in Your application context config, when properly set it skips controller for requests matching given mapping, all resources are just send to client "as it is".

It can be configured also by annotation: https://stackoverflow.com/a/14862729/2718510

Community
  • 1
  • 1
Maciej Czarnecki
  • 158
  • 2
  • 10