2016-03-16 85 views
1

我正在創建一個基本的hello world Spring MVC應用程序。但它無法找到URL映射。我搜索了很多,但沒有找到任何令人滿意的解決方案。我在控制檯上得到以下錯誤。Spring MVC在DispatcherServlet中使用URI [/HelloWeb/WEB-INF/jsp/hello.jsp]找到名爲'servletname'的HTTP請求沒有找到映射

org.springframework.web.servlet.PageNotFound noHandlerFound警告: 否[/HelloWeb/WEB-INF/jsp/hello.jsp]在DispatcherServlet的發現HTTP請求與URI 與名稱映射 'HelloController中'

任何人都可以幫我解決這個問題。以下是我的代碼

HelloController.java package com.tutorialspoint;

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

@Controller 
public class HelloController{ 

    @RequestMapping(value="/" , method = RequestMethod.GET) 
    public String printHello(ModelMap model) { 
     model.addAttribute("message", "Hello Spring MVC Framework!"); 

     return "hello"; 
    } 

} 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
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>HelloWeb</display-name> 
    <servlet> 
     <servlet-name>HelloController</servlet-name> 
     <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

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

HelloController中-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 

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

    <context:component-scan base-package="com.tutorialspoint" /> 



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

的hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>See this first MVC page here</title> 
</head> 
<body> 
<h2>${message}</h2> 
<h6>Check if above sentence is visible</h6> 
</body> 
</html> 
+0

發佈應用程序目錄結構 –

+0

請看看這個答案http://stackoverflow.com/a/32931296/584420 –

+0

包(com.tutorialspoint )> HelloController.java和webcontent結構是WEB-INF> jsp> hello.jsp也是WEB-INF> HelloDispatcher-servlet.xml,web.xml –

回答

0

有些事情我有點困惑。

首先,你不應該在web.xml中將你的servlet命名爲你的控制器。將其命名爲其他內容,但可以使用您的spring-config-xml文件進行匹配。因此,也許 <servlet> <servlet-name>HelloDispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet>

然後命名您的currrent HelloController-servlet.xml到HelloDispatcher-servlet.xml。

其次,確定HelloController-servlet.xml位於/ WebContent/WEB-INF文件夾中?第三,我不確定你使用什麼URL來訪問你的測試應用程序,但我不是100%確定你只能映射/,它可能需要做/ *或* .html,然後嘗試with /hello.html

+0

是的所有的XML都在'/ WebContent/WEB-INF'文件夾結構下。 我的項目名稱是「HelloWeb」,所以URL是「http:// localhost:8080/HelloWeb /」。 還是沒有運氣 –

0

現在好了,終於我找到了我對上述問題的回答。由於該錯誤表示無法找到確實存在的URL映射,儘管它沒有運行。問題的根本原因是主控制器類未在「HelloDispatcher-servlet.xml」中定義。

<bean class="com.tutorialspoint.HelloController" /> 

控制器接受請求並根據使用的GET或POST方法調用相應的服務方法。服務方法將根據定義的業務邏輯設置模型數據,並將視圖名稱返回給DispatcherServlet。

DispatcherServlet會採取幫助從ViewResolver來把拾取器的已定義視圖的請求

這就是爲什麼它是強制性的,以包括在「HelloDispatcher-的servlet控制器類。XML」

謝謝大家對節約的時間來幫助我:)

相關問題