无法在第一次请求时加载资源

时间:2013-11-07 15:21:13

标签: java spring spring-mvc

所以这对我来说真的很奇怪。我正在编写这个Spring应用程序,当我启动服务器并第一次导航到http://localhost/ims/resources/index.html时,它会给我以下错误:

SEVERE: Servlet.service() for servlet [appServlet] in context with path [/ims] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:848)
    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:728)
    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:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    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:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    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)

但是,如果我刷新页面,我没有任何问题。它只发生在第一个请求上。这就是我的资源配置:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.mypackage.ims.web")
public class IMSAppConfig extends WebMvcConfigurerAdapter
{
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        registry.addResourceHandler("/resources/**").addResourceLocations("ims/resources/");
    }

    ....
}

为什么会发生这种情况的任何想法?

1 个答案:

答案 0 :(得分:0)

您需要在context.xml文件中提供基本软件包扫描详细信息和其他详细信息。 这样一旦项目部署就会加载配置。

但是在你的情况下,搜索映射网址的包只在exectution时间才知道spring,所以只有在程序第一次执行后,spring才会映射url并刷新时(点击服务器)第二次)找到网址。