java - spring mvc 3.0 HTTP 状态 404

标签 java spring tomcat spring-mvc

我知道这里早些时候有人问过这类问题,我已经经历了很多但找不到我正在寻找的解决方案....

我一直在为我的测试 spring 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,就会出现错误,如上所示。

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

谢谢

问候,

哈沙德

最佳答案

您的 Controller 当前未注册为处理程序。您需要通过声明

来配置应用程序的 MVC 端
<mvc:annotation-driven/>

在您的 servlet 上下文配置中。添加名为 mvc-dispatcher-servlet.xml 的文件作为 Spring bean 配置,并将该行添加到其中。也不要忘记命名空间声明。

关于java - spring mvc 3.0 HTTP 状态 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22736103/

相关文章:

java - Hibernate 运行不需要的更新语句

java - 我如何使用 Spring 的 RestTemplate 执行与此 curl 命令等效的操作?

java - Spring MVC 如何在我的 Controller 方法中获取 GET HTTP 请求的参数值?

java - 代码不执行并通过 setWeekDate

java - JSP 导入类实现不解析类型

java - 在给定的 40 亿个整数中找到一个不存在的整数

c# - 如何从 wcf ksoap2 响应中填充字典

Java 1.8、tomcat 8.5、Jersey 2 - 无法正常工作

java - 如何在 Android 操作系统中通过 Java 访问设备驱动程序?

java - while循环与mysql java数据库