簡體   English   中英

Spring MVC webapp 未在瀏覽器上加載

[英]Spring MVC webapp not loading on browser

我是 spring mvc 和 tomcat 的新手。我開發了一個演示 spring mvc 項目,並嘗試將其部署在 tomcat 9 到 eclipse 上。服務器成功啟動,但是當我嘗試使用以下 iget 瀏覽器訪問 884101389 消息時出現錯誤. :

消息請求的資源 [/spring-mvc-demo/] 不可用

說明 源服務器未找到目標資源的當前表示或不願透露該表示的存在。

以下是我的代碼詳細信息:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">

<display-name>spring-mvc-demo</display-name>

<absolute-ordering />

<!-- Spring MVC Configs -->

<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/spring-mvc-demo</url-pattern>
</servlet-mapping>

</web-app>

spring-mvc-demo-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="main.webapp" />

<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>

<!-- Step 5: Define Spring MVC view resolver -->
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".jsp" />
</bean>

</beans>

家庭控制器.java

package main.webapp.springdemo.controller;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
 public class HomeController {

@PostConstruct
public void init() {
    System.out.println("HomeController bean getting intiated");
}

@RequestMapping("/")
public String getMainMenu() {
    return "main-menu";
}
}

我嘗試在 tomcat 9 到 eclipse 上以及手動運行此應用程序,但在這兩種情況下我都遇到了相同的錯誤。

假設應用程序部署沒有錯誤(檢查您的日志),您用來訪問它的 URI 路徑幾乎肯定是錯誤的。 在 servlet 環境中,URI 路徑分解為:

<context-path><servlet-path><path-info>

在哪里:

  • <context-path>是您的應用程序的前綴。 在 Eclipse 服務器配置頁面中,這僅稱為“路徑” ,默認為/<project-name>
  • <servlet-path>通過web.xml部署描述符中的<servlet-mapping>元素進行配置,
  • <path-info>是 Spring 通常用來執行其內部路由的部分(除非在HandlerMapping上設置了alwaysUseFullPath )。

因此(理論上)您應該嘗試訪問:

http://localhost:8080/projectName/spring-mvc-demo/

然而,還有另一個問題:您的 servlet 映射是一個精確映射,與/projectName/spring-mvc-demo之外的任何其他內容都不匹配。 您應該將其替換為前綴映射(有關 servlet 映射的概述,請參閱此問題):

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/spring-mvc-demo/*</url-pattern>
</servlet-mapping>

如果你想縮短你的 URL, DispatcherServlet通常被映射為默認的 servlet / 請注意不要使用包羅萬象的前綴映射/* ,這會覆蓋 JSP servlet 的映射。

備注:在某些情況下,Spring 不使用<servlet-path>之后的部分進行路由,例如,當您使用精確映射或擴展映射時,使用<context-path>之后的整個路徑。

因此,如果您使用:

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/spring-mvc-demo</url-pattern>
</servlet-mapping>

你應該指定:

@RequestMapping("/spring-mvc-demo")
public String getMainMenu() {
   ...
}

並使用 URL http://localhost:8080/appName/spring-mvc-demo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM