您好!
正如标题中所述,我试图将我的视图解析器配置从经典xml导出到java conf。 (我不认为我会因为这个选择而进入......?)
我之前的说法是:
的web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
...
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
dispatcher-servlet.xml:
<context:component-scan base-package="ca.mat8j8.controller" />
<!-- freemarker config -->
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/views/"/>
</bean>
<!--
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 class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".ftl"/>
</bean>
这样,它没有问题。
现在我尝试摆脱我的xml conf:
web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
...
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--Configure DispatcherServlet to use AnnotationConfigWebApplicationContext instead of default XmlWebApplicationContext.-->
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<!--Custom web configuration annotated class. -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>ca.mat8j8.configuration.WebConfiguration</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
我清空dispatcher-servlet.xml,并创建WebConfiguration:
@Configuration
@ComponentScan(basePackages = { "ca.mat8j8.controller" })
public class WebConfiguration extends WebMvcConfigurationSupport {
@Bean
public ViewResolver viewResolver() {
final FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
resolver.setCache(true);
resolver.setPrefix("");
resolver.setSuffix(".ftl");
return resolver;
}
@Bean
public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
factory.setTemplateLoaderPath("/WEB-INF/views/");
final FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setConfiguration(factory.createConfiguration());
return configurer;
}
}
在启动时,我可以看到它通过我的java webConf(断点)。 在我的脑海中,它应该做同样的事情,但是当我现在尝试到达我的家时&#34; view(使用xml conf),我觉得非常有趣:
GRAVE: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Could not resolve view with name 'home' in servlet with name 'dispatcher'] with root cause
javax.servlet.ServletException: Could not resolve view with name 'home' in servlet with name 'dispatcher'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1211)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
.....
有关信息,我使用tomcat 8,java 8和spring 4.1.5。
感谢您到目前为止阅读,提前感谢您的答案; - )