2014-09-21 162 views
0

我是新的Spring MVC,我試圖運行我的第一個應用程序,但出現以下錯誤。我不知道什麼是錯我的代碼,但我得到了以下錯誤:找不到具有名稱爲'spring-dispatcher'的DispatcherServlet中具有URI [/ FirstSpringMVCApp /]的HTTP請求的映射

No mapping found for HTTP request with URI [/FirstSpringMVCApp/] in DispatcherServlet with name 'spring-dispatcher' 

這裏是我的web.xml

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

    <servlet> 
     <servlet-name>spring-dispatcher</servlet-name> 
     <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>spring-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring-dispatcher-servlet.xml</param-value> 
    </context-param> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
</web-app> 

這裏是我的調度文件:

<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.spring.test"></context:component-scan> 

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

</beans> 

這是我的控制器

package com.spring.test; 

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 

public class HelloController { 

    @RequestMapping("/hellospring") 
    public String printHello(ModelMap model){ 
     model.addAttribute("message", "Hello spring..."); 
     return "hello"; 
    } 
} 

H ere是我的jsp文件:

<html> 
<head></head> 
<body> 
    <h1>Message : ${message}</h1> 
</body> 
</html> 

任何幫助?

回答

0

假設FirstSpringMVCApp您的應用程序的名字和你在其中的端口8080上運行,那麼你應該使用下面的URL來運行應用程序的服務器部署應用程序

localhost:8080/FirstSpringMVCApp/hellospring 
+0

並非完全*運行應用程序*,而是*作爲您的'@ RequestMapping'聲明url(相對於上下文)'/ hellospring'命中控制器*。 – 2014-09-21 13:51:22

0

解決它:

@RequestMapping(value="/") 
public String gotoIndex(){ 
return "index"; 
} 

及其工作!

+0

你應該考慮解釋你的答案,而不是隻發佈代碼。 – baao 2014-11-13 16:36:32

相關問題