【问题标题】:How can I create a webapp with Spring (Tomcat server) and Maven?如何使用 Spring(Tomcat 服务器)和 Maven 创建 webapp?
【发布时间】:2021-01-13 03:15:15
【问题描述】:

我在运行我的应用程序时收到下面的控制台消息,但每次我导航到 http://localhost:8080/?我收到一条错误消息:

“白标错误页面 此应用程序没有显式映射 /error,因此您将其视为后备。

2021 年 1 月 12 日星期二 22:04:40 EST 出现意外错误(类型=未找到,状态=404)”

控制台输出:

2021-01-12 22:03:17.212  INFO 21710 --- [           main] com.crd.carrental.CarRentalApplication   : No active profile set, falling back to default profiles: default
2021-01-12 22:03:18.343  INFO 21710 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-01-12 22:03:18.350  INFO 21710 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-01-12 22:03:18.350  INFO 21710 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.39]
2021-01-12 22:03:18.424  INFO 21710 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-01-12 22:03:18.424  INFO 21710 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1073 ms
2021-01-12 22:03:18.539  INFO 21710 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'clientInboundChannelExecutor'
2021-01-12 22:03:18.542  INFO 21710 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'clientOutboundChannelExecutor'
2021-01-12 22:03:18.548  INFO 21710 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'messageBrokerTaskScheduler'
2021-01-12 22:03:18.613  INFO 21710 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'brokerChannelExecutor'
2021-01-12 22:03:19.539  INFO 21710 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-01-12 22:03:19.541  INFO 21710 --- [           main] o.s.m.s.b.SimpleBrokerMessageHandler     : Starting...
2021-01-12 22:03:19.541  INFO 21710 --- [           main] o.s.m.s.b.SimpleBrokerMessageHandler     : BrokerAvailabilityEvent[available=true, SimpleBrokerMessageHandler [DefaultSubscriptionRegistry[cache[0 destination(s)], registry[0 sessions]]]]
2021-01-12 22:03:19.541  INFO 21710 --- [           main] o.s.m.s.b.SimpleBrokerMessageHandler     : Started.
2021-01-12 22:03:19.551  INFO 21710 --- [           main] com.crd.carrental.CarRentalApplication   : Started CarRentalApplication in 2.977 seconds (JVM running for 5.797)
2021-01-12 22:04:18.616  INFO 21710 --- [MessageBroker-1] o.s.w.s.c.WebSocketMessageBrokerStats    : WebSocketSession[0 current WS(0)-HttpStream(0)-HttpPoll(0), 0 total, 0 closed abnormally (0 connect failure, 0 send limit, 0 transport error)], stompSubProtocol[processed CONNECT(0)-CONNECTED(0)-DISCONNECT(0)], stompBrokerRelay[null], inboundChannel[pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], outboundChannel[pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], sockJsScheduler[pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 0]
2021-01-12 22:04:23.920  INFO 21710 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-01-12 22:04:23.920  INFO 21710 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-01-12 22:04:23.929  INFO 21710 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 9 ms```

【问题讨论】:

  • 能不能把控制器发给我,这样调试会更方便

标签: java spring maven tomcat


【解决方案1】:

确保您已为请求的 URL "/" 定义了一个 Controller,如下所示:

@RequestMapping("/")
public String homePage() {
    ... <your code>
    return "home";
}

而且,您的home 页面必须存储在以下路径中:

|--resources
    |--templates
        |--home.html

【讨论】:

    【解决方案2】:

    当您正在搜索该 URL 没有页面时会出现白标签错误。所以在你的控制器中看看是否有“/”的映射。

    【讨论】:

      【解决方案3】:

      白色错误标签是没有任何控制器路径的空 Spring 应用程序的预期行为。除非您尚未配置任何 REST 路径或端点,否则将显示此错误。

      如果错误仍然存​​在,请确保您的主应用程序类扩展了 SpringBootServletInitializer 并且您正在编写正确的端点路径。

      【讨论】:

        猜你喜欢
        • 2016-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-15
        • 1970-01-01
        • 2014-09-25
        • 2016-03-08
        • 1970-01-01
        相关资源
        最近更新 更多