2013-01-21 80 views
4

我正在嘗試一個Web應用程序的Spring框架,似乎無法與一個簡單的Hello世界MVC的Web應用程序的工作。使用Spring 3.2,Tomcat 6(上下文設置爲「/ spring」)。也許註釋的Controller類沒有找到?春天你好世界Web應用程序

我打http://localhost:8080/spring/hello

的web.xml

<?xml version="1.0" encoding="ISO-8859-1"?> 

<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" > 
    <display-name> 
     Spring 
    </display-name> 
    <description> 
    Spring Test 
    </description> 

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

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



</web-app> 

springapp-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="com.test.web" /> 
    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 
</beans> 

我的控制器

package com.test.web; 

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


@Controller 
@RequestMapping("/hello") 
public class HelloController { 

    @RequestMapping(method = RequestMethod.GET) 
    public String printHello(ModelMap model) { 
     model.addAttribute("name", "Test"); 
     return "hello"; 
    } 


} 

Tomcat的輸出(springapp-的servlet .XML是發現和加載)

INFO: Deploying configuration descriptor spring.xml 
Jan 20, 2013 10:22:27 PM org.apache.catalina.core.ApplicationContext log 
INFO: Initializing Spring FrameworkServlet 'springapp' 
Jan 20, 2013 10:22:27 PM org.springframework.web.servlet.FrameworkServlet initServletBean 
INFO: FrameworkServlet 'springapp': initialization started 
Jan 20, 2013 10:22:27 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh 
INFO: Refreshing WebApplicationContext for namespace 'springapp-servlet': startup date [Sun Jan 20 22:22:27 EST 2013]; root of context hierarchy 
Jan 20, 2013 10:22:27 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/springapp-servlet.xml] 
Jan 20, 2013 10:22:27 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 
INFO: Pre-instantiating singletons in org.s[email protected]7897aaa6: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy 
Jan 20, 2013 10:22:27 PM org.springframework.web.servlet.FrameworkServlet initServletBean 
    INFO: FrameworkServlet 'springapp': initialization completed in 321 ms 
Jan 20, 2013 10:22:29 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound 
WARNING: No mapping found for HTTP request with URI [/spring/hello] in DispatcherServlet with name 'springapp' 

回答

5

您還沒有啓用annotated MVC configuration

<mvc:annotation-driven /> 

例:

<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" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xsi:schemaLocation="http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
        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="..." /> 

<mvc:annotation-driven /> 

編輯:
這是與應用程序的類路徑問題因爲它原來在下面的評論

+0

我在哪裏添加它?試圖在開始時將它添加到springapp-servlet.xml中... org.xml.sax.SAXParseException:元素「mvc:annotation-driven」的前綴「mvc」未綁定。 – user979051

+0

您需要添加'xmlns:mvc =「http://www.springframework.org/schema/mvc」'和'http://www.springframework.org/schema/mvc http://www.springframework。 org/schema/mvc/spring-mvc-3.2.xsd'如圖所示 –

+0

與開始時相同的問題...警告:在名爲'springapp'的DispatcherServlet中找到URI爲URI [/ spring/hello]的HTTP請求的映射 – user979051