简体   繁体   中英

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. whenever I hit " http://127.0.0.1:8080/com.test.andro/androTest1.jsp " via browser. 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-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;
        }

    }

I'm using actual tomcat server and not one from eclipse workspace. 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.

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:annotation-driven/>

in your servlet context configuration. Add a file named mvc-dispatcher-servlet.xml as a Spring bean configuration and add that line to it. Don't forget the namespace declarations as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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