Java FreeMarkerViewResolver和普通的html视图

时间:2015-08-27 21:32:51

标签: java html spring freemarker

我在尝试使用简单的静态普通html和带有freemarker的ftl同时工作时遇到了一些麻烦。

我有Spring 4.0.2.RELEASE和以下Web配置

@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/");
    }
}

我必须导入遗留配置,其中包括xml上的freemarker

<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" />

一个简单的控制器

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

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

}

添加xml配置时,此控制器停止工作。如果我在xml中评论FreeMarkerViewResolver,它会再次开始工作。

根据我的理解,视图解析器正处于ResourceHandlerRegistry和请求的中间,返回无法解析名称为&#39; /static/html/index.html'的视图;在名为&#39;调度员&#39;

的servlet中

有没有办法让静态html和freemarkerviewresolver共存?

1 个答案:

答案 0 :(得分:0)

我会在您的应用程序上下文配置中尝试使用<mvc:resources mapping="/your_static_resources_mapping/**" location="/your_static_resources_folder/"/>指令,正确设置它会跳过控制器以查找与给定映射匹配的请求,所有资源都只是“按原样”发送给客户端。

它也可以通过注释配置: https://stackoverflow.com/a/14862729/2718510