2014-09-21 164 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

相关问题