简体   繁体   中英

Why am I getting a 404 error when accessing my Spring MVC app URL?

I'm following a tutorial at Udemy for my first Spring-MVC web app. I've configured it using XML for the servlet specification exactly as controlled with a Controller class to return a String. Navigating to the URL http://localhost:8080/spring-mvc/login I get a 404. My code:

web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>Spring To do List</display-name>
<!-- 
    <welcome-file-list>
        <welcome-file>login.do</welcome-file>
    </welcome-file-list>
     -->
         <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/todo-servlet.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/spring-mvc/*</url-pattern>
        </servlet-mapping>
</web-app>

todo-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.wds" />
    
    <mvc:annotation-driven/>

</beans>

''' LoginController.java ''' package com.wds.springmvc;

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

@Controller
public class LoginController {
    
    @RequestMapping(value = "/login")
    @ResponseBody
    public String sayHello() {
        System.out.println("performing hello");
        return "hello world";
    }
}

pom.xml:

4.0.0 com.in28minutes in28Minutes-first-webapp 0.0.1-SNAPSHOT war

<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.2.2.RELEASE</version>
    </dependency>
</dependencies>
<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <source>1.8</source>
                    <target>1.8</target>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <path>/</path>
                    <contextReloadable>true</contextReloadable>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

My understanding is that if the controller method executes the Dispatcher Servlet should return the view as specified in the servlet config. As no is specified in there it should just return the String from the controller method ie "hello world". Instead I get the 404. What I'm confused about is in the console, I can see the servlet config has been registered and the "say Hello" controller method mapped to "/login". I've put a System out in the controller method but it's not being output.

Any idea why I get the 404?

Many Thanks

I would suggest to use request mapping on top of the controller

@Controller
@RequestMapping("/spring-mvc")
public class TestController {

    @RequestMapping("/login")
    @ResponseBody
    public String test(){
        System.out.println("test is running");
        return "test is working";
    }
}

and in web.xml

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

Advantages: Good to read the code as well as easy to scale the application

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