2016-02-19 51 views
4

我查找了几乎所有关于该错误的主题,但没有一个对我有用。在名为xx的DispatcherServlet中找到具有URI xxx的HTTP请求没有找到映射

我创建了一个简单的Spring MVC项目,当我运行该项目,我得到这个错误:

févr. 19, 2016 12:29:56 PM org.springframework.web.servlet.PageNotFound noHandlerFound 
AVERTISSEMENT: No mapping found for HTTP request with URI [/SpringMVCsample1/] in DispatcherServlet with name 'one' 

这里是我的代码:

的web.xml:

一个-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"> 

    <!-- This will allow Spring to load all the components from package com.capgemini.springtest --> 
    <!-- and all its child packages. --> 
    <context:component-scan base-package="com.capgemini.springtest" /> 

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

</beans> 

OneController.java:

package com.capgemini.springtest; 


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 OneController { 

    @RequestMapping(value = "/hello", method = RequestMethod.GET) 
    public String printHello(ModelMap model) { 

     model.addAttribute("message", "Hello Spring MVC Framework!"); 

     return "hello"; 
    } 

} 

的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>Insert title here</title> 
</head> 
<body> 
    <h2>${message}</h2> 
</body> 
</html> 

任何想法?

+0

那么你的电话应该是SpringMVCsample1 /你好,而不是SpringMVCsample1 /一个 –

+0

你引用了什么电话? – FrankelStein

回答

1

当然你会有这样的问题,因为你没有任何控制器来处理“/”的请求,你要做的就是添加一个控制器或者在你的web文件夹下添加index.jsp

web 
|--->WEB-INF 
| |--->jsp 
|  |---->index.jsp 
|--->index.jsp 
+0

这是我的文件夹组织: | ---> WEB-INF | | ---> jsp | | ----> hello.jsp – FrankelStein

+0

在WEB-INF旁边创建index.jsp – achabahe

+0

不在里面 – achabahe

0

web.xml您的servlet仅映射/ URI。网址格式应该是/*

+0

已经尝试过,仍然有相同的错误。 – FrankelStein

+0

让我感到困惑的是错误消息中提到的'/ SpringMVCsample1 /'URI。这就像它被认为是(或应该)以'/ hello'而不是Web应用程序名称终止的URI的一部分。你的应用程序在你的应用程序服务器中部署了哪个名称,以及用来调用你的'/ hello'页面的完整URL是什么? –

+0

我的应用程序部署在:http:// localhost:8080/SpringMVCsample1/ 和/ hello路径是未知的,404 ... – FrankelStein

2

更改URL模式在网络的xml:

<url-pattern>/*</url-pattern> 

改变控制器具有根上下文的映射:

package com.capgemini.springtest; 


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 OneController { 

    @RequestMapping(value = {"/","/hello"}, method = RequestMethod.GET) 
    public String printHello(ModelMap model) { 

     model.addAttribute("message", "Hello Spring MVC Framework!"); 

     return "hello"; 
    } 

} 

hello.jsp中应该驻留在文件夹中:/ WEB-INF/JSP/hello.jsp

+0

在文件夹下有hello.jsp:/WEB-INF/jsp/hello.jsp帮助,但为什么它应该在该文件夹下?还有,$ {message}不显示任何内容,为什么? – FrankelStein

+0

因为你提到过你的jsp将驻留在这个文件夹/ WEB-INF/jsp/one-servlet.xml中:

0

尝试将<mvc:annotation-driven />加入one-servlet.xml。请记住在那里添加mvc名称空间的定义以及xmlns:mvc="http://www.springframework.org/schema/mvc"

然后尝试访问localhost:8080/SpringMVCsample1/hello。不是localhost:8080/SpringMVCsample1/hello/像你说你已经尝试过其中一个评论

+0

已经存在。不需要

+0

@ kunal-surana我认为'context:component-scan'用于声明包含组件的包和子包。Spring可以自动装配。 'mvc:annotation-driven'用于识别诸如“@ Controller”,“@ RequestMapping”,“@ ResponseBody”等注释。 –

+0

按照您的说法进行操作,但没有解决问题。 – FrankelStein

相关问题