2016-03-20 40 views
0

HTTP请求。当我试图运行我的春天web应用程序,我收到以下错误映射:警告:未找到与URI

org.springframework.web.servlet.DispatcherServlet noHandlerFound 
WARNING: No mapping found for HTTP request with URI [/HibwenateWeb/index] in DispatcherServlet with name 'customerdispatcher 

我无法载入我的主页。我经历了很多关于错误的文章,大多数人都建议在调度器中添加<mvc:default-servlet-handler />,但添加后我没有收到无处理程序错误,但主页仍未加载。我不知道这个问题..任何人都可以帮助我吗?

我控制器

package com.springforbeginners.controller; 

import java.util.Map; 

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

@Controller 
public class CustomerController { 
    @RequestMapping(value="/index") 
    public String listCustomers(ModelAndView mav) { 
     System.out.println("Hi i am in controller"); 
     //map.put("customer", new Customer()); 
     //map.put("customerList", customerService.listCustomer()); 
     mav.setViewName("customer"); 
     return "customer"; 
    } 

} 

customerdispatcher-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:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/tx" 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.1.xsd 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.1.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
    <mvc:annotation-driven /> 
    <context:annotation-config /> 
    <context:component-scan base-package="com.springforbeginners" /> 

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

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" 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_3_0.xsd"> 
    <servlet> 
     <servlet-name>customerdispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>customerdispatcher</servlet-name> 
     <url-pattern>/index</url-pattern> 
    </servlet-mapping> 
    <session-config> 
     <session-timeout>30</session-timeout> 
    </session-config> 

</web-app> 
+1

在你的web.xml,使用'/',而不是' /指数'。 – nnunes10

+0

@Mohan我怀疑这可能是因为'/index'和'RequestMapping(value =“/ index”)'都使用'index'。你可以尝试'/*'? –

+0

@Madhusudana Reddy Sunnapu尝试使用url-pattern>/* ..仍然是相同的问题..没有找到处理程序.. – Mohan

回答

0

我在你的结构写了一个很简单的PRJ,在这儿呢。下 POM依赖性

<dependencies> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>servlet-api</artifactId> 
      <version>2.5</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>4.2.5.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 
    </dependencies> 

的web.xml下WEB-INF

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" 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_3_0.xsd"> 
    <display-name>MyFirstSpringMVC</display-name> 
    <servlet> 
     <servlet-name>customerdispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>customerdispatcher</servlet-name> 
     <url-pattern>/index/*</url-pattern> 
    </servlet-mapping> 
    <session-config> 
     <session-timeout>30</session-timeout> 
    </session-config> 
</web-app> 

customerdispatcher-servlet.xml中WEB-INF

<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.*"/> 
    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value>/WEB-INF/jsp/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 
    <!-- <mvc:annotation-driven /> --> 
</beans> 

customer.jsp下/ WEB-INF/JSP/

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<!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=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 
this is customer.jsp 
</body> 
</html> 

CustomerController

package com.springforbeginners.controller; 

import java.util.Map; 

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

@Controller 
public class CustomerController { 

    @RequestMapping(value="/index") 
    public String listCustomers(ModelAndView mav) { 
     System.out.println("Hi i am in controller"); 
     return "customer"; 
    } 
} 

这就是结果。 http://imgur.com/P51TSbk

由于您以默认方式(“dispatcher name”+“ - servlet.xml”)声明customerdispatcher-servlet.xml,因此您不需要添加spring contextloader或设置init-param来使调度程序servlet的工作。

更多信息http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-servlet

希望这可以帮助你。


顺便说,宣布的url-pattern的根路径

<servlet-mapping> 
     <servlet-name>customerdispatcher</servlet-name> 
     <url-pattern>/</url-pattern> <= it's slash only, not /* 
    </servlet-mapping> 

这种细微的差别时,都让我一个小时才找到它......

+0

我改变了我所有的文件,正如你所建议的,但我仍然只得到404错误..(即没有发现处理程序)..是因为任何春天的jar版本?我使用的是spring 3.1.1版本。请问你能帮我解决这个问题吗? – Mohan

0

您必须添加将以下元素添加到您的web.xml中。

<!-- The definition of the Root Spring Container --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <!-- provide the path for your customerdispatcher-servlet.xml --> 
    <param-value>/WEB-INF/customerdispatcher-servlet.xml</param-value> 
</context-param> 

<!-- Creates the Spring Container shared by all Servlets and Filters --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

此外,你必须到url-pattern元素改为:

<url-pattern>/</url-pattern> 
+0

我已经在web.xml中添加了上下文参数和监听器,但我没有得到我的主页..仍然是同样的问题..(处理程序未找到..) – Mohan

+0

什么是您的软件包名称.war /.ear? – nnunes10

+0

它只是.war .. – Mohan

相关问题