0

当我点击 Spring 3.2 应用程序的 /calendar URL 时,CONtroller 代码被成功点击,并且视图“日历”在 ModelAndView 对象中设置。

我从春天收到一个关于无法解析视图“日历”的错误,它应该指向 /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 问题。

我那里的配置正确吗?

编辑/更新:刚刚注意到,每次刷新页面时,对 /calendar 的请求似乎都会发生两次。也许那里有一些循环引用?

4

1 回答 1

1

我似乎已经修复了它......至少我现在得到了一个来自 FTL 的页面。

如果有人偶然发现这个问题,我会添加一些注释。

所做的更改:

@Bean(name="freemarkerConfig")
public FreeMarkerConfig freeMarkerConfig() {

就是现在...

@Bean
public FreeMarkerConfigurer freeMarkerConfig() {

注意在返回的类型中添加了“urer”!

我删除了该方法中的几乎所有配置,因此该方法现在很简单:

FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("/WEB-INF/views/");
return configurer;

在某个时候,我会恢复一些东西。

因为我在此方法中删除了对 COnfiguration 类的引用,所以我可以重新组织我的导入并在该类上使用 @Configuration 而不是完全限定的 spring..Configuration 类。

于 2013-07-17T22:53:56.387 回答