获得nohandler在Spring MVC中发现了异常。

时间:2012-03-01 09:47:11

标签: spring spring-mvc

我正在尝试使用Spring MVC运行一个简单的Web应用程序。我有一个页面,我想最终使用控制器类登陆并显示一条消息。但我不知道我总是得到没有处理程序发现异常。你可以找出我的代码,让我知道我哪里出错了。 ?请在Spring MVC中解释一下这些注释的概念,基本上什么时候使用哪个注释?谢谢您宝贵的时间..

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

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

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>
      jsp/index.jsp
    </welcome-file>
  </welcome-file-list>

</web-app>

用SpringMVC-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<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" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


     <context:component-scan base-package="src.springmvc.web"/> 
      <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>


  <!-- <bean name="/hello_world.html" class="springmvc.web.HelloWorldController"/>
   <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
  <bean name="*/Start.html" class="springmvc.web.Example"/>-->


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

</beans>

Example.java

package springmvc.web;

import org.apache.jasper.tagplugins.jstl.core.Redirect;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Example {
    @RequestMapping("/Springapp2/jsp/Start.html")
    public ModelAndView method(){
        String hello = "Message";
        ModelAndView obj = new ModelAndView("end");
        obj.addObject("hello", hello);
        return obj;

    }

}

End.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
     <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>
Ending page.. !! 

This is the end page :: ${hello}
</body>
</html>

Stacktrace:

Mar 01, 2012 3:07:26 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for [/Springapp2/jsp/Start.html] in DispatcherServlet with name 'springmvc'

1 个答案:

答案 0 :(得分:1)

我认为您的扫描基本路径存在问题

<context:component-scan base-package="src.springmvc.web"/> 

但您的控制器包是

package springmvc.web;