2015-11-19 95 views
0

我學習Spring MVC和因爲在這個問題了幾個小時,我阻止,應該有一個明顯的分辨率:警告:未找到HTTP請求的URI與org.springframework.web.servlet.PageNotFound noHandlerFound映射

  • 我在web.xml中定義了DispatcherServlet springSoccer並在SpringSoccer-servlet.xml中的WEB-INF目錄下進行配置。

  • 在springSoccer-servlet.xml中我配置ViewResolver,設置指向我控制器包的組件掃描。

我部署到Tomcat 8.0,當我指出我的瀏覽器http://localhost:8080/SoccerSpringMaven3/springSoccer/users/我收到錯誤:

Nov 19, 2015 9:07:50 PM org.springframework.web.servlet.PageNotFound noHandlerFound 
WARNING: No mapping found for HTTP request with URI [/SoccerSpringMaven3/springSoccer/users] in DispatcherServlet with name 'springSoccer' 

這只是一個沒有找到我的RequestMapping的問題。找到配置如下:

的web.xml

<!DOCTYPE web-app PUBLIC 
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd" > 

<web-app> 
    <display-name>Archetype Created Web Application</display-name> 


    <!-- Source project: sip05, branch: 01 (Maven Project) --> 
    <servlet> 
     <servlet-name>springSoccer</servlet-name> 
     <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 

     <load-on-startup>1</load-on-startup> 
    </servlet> 


    <servlet-mapping> 
     <servlet-name>springSoccer</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 


</web-app> 

springSoccer-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" 
    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"> 


    <context:component-scan base-package="com.spring.soccer.controller" /> 

    <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:resources mapping="/resources/**" location="/resources/" /> 

    <mvc:annotation-driven /> 

</beans> 

SoccerController.xml

package com.spring.soccer.controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Controller 
public class SoccerController { 

    @RequestMapping("/users") 
    public String printSoccerHome(ModelMap model) { 
     return "HelloSoccer"; 
    } 
} 

任何指針?非常感謝。

+0

註解你的方法,你不應該來註解你'SoccerController'用' @RequestMapping(「/ SoccerSpringMaven3/SpringSoccer」)'爲了這個工作?或直接調用'http:// localhost:8080/users'?調用'HandlerMapping'時,'DisptatcherServlet'找不到'SoccerSpringMaven3 /'映射。 – RK1

+0

案件事宜!!!!!!!!!!!!!試試/ SoccerSpringMaven3/springSoccer /用戶 – jny

+0

RK1謝謝。我嘗試過,但它不起作用,我的理解是,首先獲取URI的上下文根,然後是servlet名稱,然後是我放入RequestMapping的任何東西。我在POM中定義的上下文根是 SoccerSpringMaven3那麼ServletNAme是springSoccer。 –

回答

0

<servlet-name>只定義哪個DispatcherServlet處理特定的請求。它不起作用,如@RequestMapping註釋。您只配置了Spring,以便全部請求由springSoccerDispatcherServlet處理。

閱讀Spring MVC documentation- 21.2 The DispatcherServlet瞭解更多信息。

爲了這個要求的工作:

http://localhost:8080/SoccerSpringMaven3/springSoccer/users/

你必須要麼@RequestMapping("/springSoccer")註釋您的控制器或@RequestMapping("/springSoccer/users")

相關問題