2014-03-29 96 views
0

我知道这类型的问题都是较早这里,我经历了许多走了,但找不到我在寻找....Spring MVC的3.0 HTTP状态404

的解决方案我保持为我的测试弹簧mvc 3.0应用程序获取“HTTP状态404”。每当我通过浏览器打“http://127.0.0.1:8080/com.test.andro/androTest1.jsp”时。这是简单的应用程序。它应该读取文件的第一行文本,并返回内容作为响应。 下面是配置文件,有人可以请弄清楚这里出了什么问题。

------------------------------- web.xml ------------ -------------------

<web-app id="WebApp_ID" 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>Archetype Created Web Application</display-name> 

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

    <servlet-mapping> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 

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

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

    <context-param> 
    <param-name>log4jConfigLocation</param-name> 
    <param-value>WEB-INF/classes/log4j.properties</param-value> 
    </context-param> 

    <listener> 
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
    </listener> 

</web-app> 

------------------------ --------- mvc-dispatcher-servlet.xml ---------------------------------- ----

<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.andro" /> 



    </beans> 

--------------------------------------- --Controller ---------------------------------------

package com.test.controller; 

    import java.io.BufferedReader; 
    import java.io.File; 
    import java.io.FileReader; 

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

    @Controller 
    public class AndroController { 

     @RequestMapping(value="/androTest1.jsp", method=RequestMethod.GET) 
     @ResponseBody 
     public String readFromTestFile() { 

      File file = new File("\resources\test1.txt"); 

      try { 

       BufferedReader bfr = new BufferedReader(new FileReader(file)); 

       return bfr.readLine(); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

    } 

我使用的是实际的tomcat服务器,而不是来自eclipse工作区的服务器。 因此,如果我点击“http://127.0.0.1:8080/”,我确实会得到tomcat服务器页面(猜测我的服务器工作正常),但如果我点击上面显示的与项目相关的URL,则会出错。

如果有人能告诉我我在这里做什么错,这将是非常有帮助的。

谢谢

问候,

Harshad

回答

0

您的控制器当前没有被注册为处理程序。您需要通过在servlet上下文配置中声明

<mvc:annotation-driven/> 

来配置应用程序的MVC端。将一个名为mvc-dispatcher-servlet.xml的文件添加为Spring bean配置并将该行添加到该文件中。不要忘记命名空间声明。

+0

嗨Sotirios,感谢您的快速回复真的很感激它。正如你所建议的那样,我已经将mvc:annotation-driven />与mvc-dispatcher-servlet.xml一起添加到了名称空间中,同时contextConfiguration已经存在于我发布在我的问题中的web.xml中。但仍然没有运气.....得到相同的错误......谢谢 –