简体   繁体   English

spring mvc 3.0 HTTP状态404

[英]spring mvc 3.0 HTTP Status 404

I know this type of questions were asked earlier here, and I have gone through many but couldn't find the solution I'm looking for.... 我知道这类问题早在这里已经问过了,我经历了很多问题,但找不到我想要的解决方案。

I'm keep on getting "HTTP Status 404" for my test spring mvc 3.0 app. 我一直在为我的测试spring mvc 3.0应用程序获取“ HTTP状态404”。 whenever I hit " http://127.0.0.1:8080/com.test.andro/androTest1.jsp " via browser. 每当我通过浏览器点击“ http://127.0.0.1:8080/com.test.andro/androTest1.jsp ”时。 This is simple app. 这是简单的应用程序。 which should read the 1st line of text from a file and should return the content as response. 它应该从文件中读取文本的第一行,并应返回内容作为响应。 Below are configuration files, can someone please figure it out what's going wrong here. 以下是配置文件,有人可以找出问题所在吗?

-------------------------------web.xml------------------------------- ------------------------------- 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 -------------------------------------- --------------------------------- 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;
        }

    }

I'm using actual tomcat server and not one from eclipse workspace. 我使用的是实际的tomcat服务器,而不是Eclipse工作区中的一个。 So if I hit " http://127.0.0.1:8080/ " I do get tomcat server page(guess my server is working properly) but get error if I hit project related URL as shown above. 因此,如果我点击“ http://127.0.0.1:8080/ ”,我会得到tomcat服务器页面(猜测我的服务器工作正常),但是如果我点击上面显示的与项目相关的URL,则会出错。

It would be really helpful if someone can tell me what's wrong I'm doing here. 如果有人可以告诉我我在这里做错了,那将非常有帮助。

Thank you 谢谢

Regards, 问候,

Harshad 哈沙德

Your controller isn't currently being registered as a handler. 您的控制器当前尚未注册为处理程序。 You'll need to configure the MVC side of the application by declaring 您需要通过声明以下内容来配置应用程序的MVC端

<mvc:annotation-driven/>

in your servlet context configuration. 在您的servlet上下文配置中。 Add a file named mvc-dispatcher-servlet.xml as a Spring bean configuration and add that line to it. 添加一个名为mvc-dispatcher-servlet.xml作为Spring bean配置,并将该行添加到其中。 Don't forget the namespace declarations as well. 也不要忘记名称空间声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM