Spring 3.1.1基于java的FreeMarkerConfigurer配置问题

时间:2012-03-24 22:35:07

标签: spring spring-mvc freemarker

我正在使用带有Freemarker的Spring 3.1.1。 在我成功使用新的基于java的新概念3.1的配置之后。 现在我尝试使用Freemarker。 但是,我得到了那个例外:

  

javax.servlet.ServletException:无法解析名称视图   '/ welcome'在名为'appServlet'的servlet中     org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1139)     org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:927)     org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)     org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)     javax.servlet.http.HttpServlet.service(HttpServlet.java:621)     javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

这是WebConfig:

package com.springway.config;

@Configuration
@ComponentScan("com.springway")
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Inject
    private Environment environment;

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new MappingJacksonHttpMessageConverter());
    }

    @Bean
    public ViewResolver viewResolver() {
        UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
        viewResolver.setViewClass(FreeMarkerView.class);
        return viewResolver;
    }

    @Bean
    public FreeMarkerConfigurer freeMarkerConfigurer() {

        FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
        configurer.setTemplateLoaderPath(
                "/WEB-INF/freemarker.xml");

        return configurer;
    }

freemarker.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">    

    <!-- freemarker config -->
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
      <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
      <property name="freemarkerSettings">
          <props>
              <prop key="number_format">0.######</prop>
          </props>
      </property>
      <property name="freemarkerVariables">
        <map>
          <entry key="xml_escape" value-ref="fmXmlEscape"/>
        </map>
      </property>
    </bean>

    <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>


    <!--

      View resolvers can also be configured with ResourceBundles or XML files. If you need
      different view resolving based on Locale, you have to use the resource bundle resolver.

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

</beans>

控制器:

@RequestMapping(value="/", method=RequestMethod.GET)
public ModelAndView home(Principal user) {

return new ModelAndView("/welcome.ftl");
}

3 个答案:

答案 0 :(得分:0)

我正在努力解决与你相同的问题。我试图为“org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer”打开日志级别“DEBUG”。我发现以下消息:

java.io.FileNotFoundException: ServletContext resource [/WEB-INF/freemarker/] cannot be resolved to URL because it does not exist at org.springframework.web.context.support.ServletContextResource.getURL(ServletContextResource.java:154) ~[spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE] at org.springframework.web.context.support.ServletContextResource.getFile(ServletContextResource.java:169) ~[spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE] at org.springframework.ui.freemarker.FreeMarkerConfigurationFactory.getTemplateLoaderForPath(FreeMarkerConfigurationFactory.java:351) [spring-context-support-3.1.1.RELEASE.jar:3.1.1.RELEASE] at org.springframework.ui.freemarker.FreeMarkerConfigurationFactory.createConfiguration(FreeMarkerConfigurationFactory.java:304) [spring-context-support-3.1.1.RELEASE.jar:3.1.1.RELEASE] .....

来自Jetty的有线日志:

WARN org.eclipse.jetty.util.log - /freemarker/

我不知道发生了什么!所以我用“/ WEB-INF / tmpl-freemarker /”替换了“templateLoaderPath”的值,然后一切都可行!!

接下来,我尝试使用ServerContext.getResource()来测试“/WEB-INF/freemarker/html.ftl”。结果如下:

Resource under /WEB-INF/freemarker/: null

我的测试代码:

@Inject
private ServletContext servletContext;

try {
    logger.info("Resource under /WEB-INF/freemarker/: {}", servletContext.getResource("/WEB-INF/freemarker/html.ftl"));
} catch(Exception e) {
    logger.info("Exception while getResource", e);
}

似乎ServletContext无法正确找到“/ WEB-INF / freemarker /”下的资源。但我不确定Spring的ServletContext的对象是否与Java Servlet的正常环境相同。

我正在使用Maven的FreeMarker 2.3.19,Springframework 3.1.1.RELEASE和jetty-maven-plugin 7.4.5.v20110725。

答案 1 :(得分:0)

@Configuration
@EnableWebMvc

@ImportResource("classpath:/security-integration.xml")
@ComponentScan("com.springway")
public class WebConfiguration extends WebMvcConfigurerAdapter {

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }


    @Bean
    public FreeMarkerViewResolver viewResolver() {
        FreeMarkerViewResolver freeMarkerViewResolver = new FreeMarkerViewResolver();
        freeMarkerViewResolver.setCache(true);
        freeMarkerViewResolver.setPrefix("");
        freeMarkerViewResolver.setSuffix(".ftl");
        return freeMarkerViewResolver;
    }

    @Bean
    public FreeMarkerConfigurer freemarkerConfig() {
        FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
        freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/freemarker/");
        Properties settings = new Properties();
        settings.setProperty("number_format", "0.######");
        freeMarkerConfigurer.setFreemarkerSettings(settings);
        Map variables = new java.util.HashMap<String, Object>();

        variables.put("xml_escape", new XmlEscape());
        freeMarkerConfigurer.setFreemarkerVariables(variables);
        return freeMarkerConfigurer;
    }
}

答案 2 :(得分:0)

您可能输错了

import org.springframework.web.portlet.ModelAndView; 

而不是这样做

import org.springframework.web.servlet.ModelAndView;