2012-03-01 112 views
0

我想用Spring MVC運行一個簡單的web應用程序。我有一個頁面,我最終想要使用控制器類並顯示一條消息。但我不知道我總是得到沒有處理程序發現異常。你可以找出我的代碼,讓我知道我哪裏錯了。 ?請在MVC中解釋這些註釋的概念,基本何時使用哪個註釋?感謝年寶貴的時間..在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> 

堆棧跟蹤:

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

我認爲你有問題,你掃描的基本路徑

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

但你的控制器封裝是

package springmvc.web; 
相關問題