2016-12-02 35 views
0

我得到了404錯誤,我的第一個spring mvc應用程序,遵循所有的配置,但沒有運氣。你能幫忙解決問題嗎?獲取org.springframework.web.servlet.PageNotFound noHandler發現錯誤

Following is the change: 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
    <display-name>HereWeGo</display-name> 


    <display-name>HereWeGo</display-name> 
    <servlet> 
     <servlet-name>spring-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    </servlet> 

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


</web-app> 

彈簧調度-servlet.xml中

<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" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.1.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> 

    <mvc:annotation-driven /> 

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
      <property name="prefix"> 
       <value>/WEB-INF/</value> 
      </property> 
      <property name="suffix"> 
       <value>.jsp</value> 
      </property> 
    </bean> 

</beans> 

HeyThere.java

package com.aditya.spring; 

import org.springframework.stereotype.Controller; 

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

import org.springframework.web.servlet.ModelAndView; 

import org.springframework.web.servlet.config.annotation.EnableWebMvc; 


@EnableWebMvc 

@Controller 

public class HeyThere{ 

     @RequestMapping("/welcome") 
     protected ModelAndView handleRequestInternal(){ 
      ModelAndView modelandview = new ModelAndView("HelloPage"); 

      modelandview.addObject("welcomeMessage","Hi user, welcome to the first spring mvc application"); 

      return modelandview; 

     } 

} 

的hello.jsp

<html> 
<body> 
    <h1>First Spring MVC Application Demo</h1> 
    <h2>${welcomeMessage}</h2> 
</body> 
</html> 

在服務器控制檯我得到以下警告:

org.springframework.web.servlet.PageNotFound noHandlerFound 

WARNING: No mapping found for HTTP request with URI [/HereWeGo/] in DispatcherServlet with name 'spring-dispatcher' 

我的項目名稱是HereWeGo。

回答

0

您的配置看起來不錯,除了@EnableWebMvc註釋(這是特定於Java的配置)來完成相同的<mvc:annotation-driven />標籤。(參考這裏EnableWebMvc annotation meaning

還修復了這個問題,如在規定註釋應該是在<mvc:annotation-driven>標記之後添加 <context:component-scan base-package="com.aditya.spring" /> spring-dispatcher-servlet.xml,用於在Spring應用程序上下文中註冊控制器。

根據你的例外情況,你的控制器被映射到/ welcome,所以你應該嘗試使用 localhost:<YourServerPort>/<YourWebAppDeployName>/welcome來輸入你的控制器方法並訪問你的jsp視圖。

+0

刪除指定的註釋並以「/ welcome」訪問,獲得相同的響應並出現以下錯誤。 ** org.springframework.web.servlet.PageNotFound noHandlerFound ** **警告:否的DispatcherServlet找到HTTP請求與URI [/ HereWeGo /歡迎]名爲 '彈簧調度' 映射** –

+0

請嘗試本地主機:8080/com.aditya.spring /歡迎如果8080是您的服務器端口,並讓我知道它的工作 –

+0

沒有運氣,多一個觀察也給404頁。 –