2017-05-25 120 views
0

我是Spring MVC的新手。我正在嘗試使用Spring MVC和Tomcat作爲服務器來創建一個簡單的Web應用程序。但是當我部署我的項目時,我得到錯誤404沒有找到。 你能幫我嗎? 這裏是我的web.xml在tomcat上未找到404的Spring MVC

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
     version="2.5"> 
    <servlet> 
     <servlet-name>spring-web</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>spring-web</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>WEB-INF/spring-web-servlet.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <welcome-file-list> 
     <welcome-file>/main/index</welcome-file> 
    </welcome-file-list> 
    </web-app> 

這裏是我的控制器:

@Controller 
@RequestMapping(value = "/main") 
    public class RootController { 
    @RequestMapping(value = "/index") 
    public String printHello(){ 
     return "index"; 
     } 
    } 

這裏是我的春天網絡servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.2.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> 
    <context:component-scan base-package="com.controllers" /> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value>/WEB-INF/views/jsp/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 
    <mvc:annotation-driven /> 
</beans> 
+0

您可以在啓動服務器後訪問tomcat服務器頁面嗎?通常'http:// localhost:8080 /'如果可以去管理器應用程序,看到你的應用程序在那裏列出。 –

+0

@ Rajith Pemabandu,不,我不能在那裏看到我的應用程序,我什麼都沒有得到http:// localhost:8080/ – Rostyk

+0

如果你只是想讓Spring MVC web應用程序快速啓動並運行,你是否考慮過Spring Boot?您可以從start.spring.io快速建立項目。如果不是,你可以發佈tomcat日誌。 –

回答

0

問題出在我項目的結構上。我錯誤地創建了它,結果出現了我的項目整個結構的正確性和文件的可見性問題。創建新項目幫助了我。

1

默認情況下,服務器查找歡迎文件按以下順序排列:

<welcome-file-list> in web.xml 
index.html 
index.htm 
index.jsp 

如果找不到這些文件,服務器呈現404錯誤。