當我點擊我的Spring 3.2應用程序的日曆URL時,控制器代碼被成功命中,並且在ModelAndView對象中設置了視圖'日曆'。我的Spring 3.2/Freemarker配置有什麼問題?
我得到一個錯誤,從春天有關無法解析View「日曆」,這應該/WEB-INF/views/calendar.ftl
春點:3.2 Freemarker的:2.3.20
我知道視圖名稱設置正確,因爲錯誤是無法解析該視圖。
我的Java的配置:
public class YhjInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.addFilter("Sitemesh", yhj.web.filters.SitemeshFilter.class).addMappingForServletNames(null, false, "/*");
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(MvcConfiguration.class);
ctx.setServletContext(servletContext);
DispatcherServlet springServlet = new DispatcherServlet(ctx);
ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", springServlet);
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
// want to load some data, eg club list for app scope here?
}
}
@ComponentScan(basePackages={"yhj"})
@org.springframework.context.annotation.Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/").addResourceLocations("/resources/css/**");
registry.addResourceHandler("/js/").addResourceLocations("/resources/js/**");
registry.addResourceHandler("/img/").addResourceLocations("/resources/img/**");
}
@Bean(name = "viewResolver")
public ViewResolver viewResolver() {
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
resolver.setCache(true);
// resolver.setPrefix("");
resolver.setSuffix(".ftl");
return resolver;
}
@Bean(name="freemarkerConfig")
public FreeMarkerConfig freeMarkerConfig() {
Properties props = new Properties();
props.setProperty("number_format","0.##");
props.setProperty("locale","en-GB");
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setFreemarkerSettings(props);
configurer.setConfiguration(configuration);
configurer.setTemplateLoaderPath("/WEB-INF/views/");
return configurer;
}
}
控制方法(我很高興的要求更是創下此方法):
@RequestMapping(value="/calendar", method=RequestMethod.GET)
public ModelAndView calendarPage(ModelAndView mv){
mv.setViewName("calendar");
mv.addObject("events",eventService.getAll());
return mv;
}
我不認爲應用程序的其他領域(例如sitemesh)正在阻礙,但如果這可能有助於診斷,我也會分享。
錯誤:
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/yhj] threw
exception [Could not resolve view with name 'calendar' in servlet with name
'dispatcher'] with root cause javax.servlet.ServletException: Could not resolve view
with name 'calendar' in servlet with name 'dispatcher'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1190)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
要我來說,似乎「日曆」從控制器沒有被轉換爲文件位置「/WEB-INF/views/calendar.ftl」,所以也許一個ViewResolver問題。
我的配置是否正確?
編輯/更新: 只要注意到,每次刷新頁面時,就會出現/日曆的請求發生兩次。那裏可能有一些循環引用?