在Spring MVC中获得警告

时间:2016-12-06 19:18:17

标签: spring jsp spring-mvc

访问网址http://localhost:8080/RiteshMVC/hey时出现此错误:

  

org.springframework.web.servlet.DispatcherServlet noHandlerFound   警告:找不到带有URI [/ SpringDemo /]的HTTP请求的映射   DispatcherServlet,名称为“SpringMVCDemo”

我已经用公共记录加载了所有罐子。

这是我的代码。

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

     <display-name>RiteshMVC</display-name>
     <servlet>
       <servlet-name>RiteshMVCDemo</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet                   </servlet-class>
       <load-on-startup>1</load-on-startup>
     </servlet>

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

</web-app>

RiteshMVCDemo-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="ritesh.dhoke" />

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

Controllers.java

package ritesh.dhoke;

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

@Controller
@RequestMapping("/hey")
public class Controllers 
{
    @RequestMapping(method=RequestMethod.GET)
    public ModelAndView Hey(){

        return new ModelAndView("hello", "User", "This is cool man");
    }
}

的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>
This is me ${User}
</body>
</html>

请帮忙。似乎是完美的,但我不知道什么是错的。提前谢谢!

1 个答案:

答案 0 :(得分:0)

async文件配置中,您没有提到spring上下文配置文件位置。所以spring将无法找到web.xml文件。您还没有注册RiteshMVCDemo-servlet.xml。请使用以下代码段:

ContextLoaderListener

<?xml version="1.0" encoding="UTF-8"?> <web-app ...> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.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> <display-name>RiteshMVC</display-name> <servlet> <servlet-name>RiteshMVCDemo</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/RiteshMVCDemo-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>RiteshMVCDemo</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> root-context.xml应位于配置

中提及的位置