简体   繁体   中英

HTTP Status 404 – Not Found is always return

I'm setting up a spring mvc project from the scratch following some tutorial. initially I configured web app to run in the tomcat. I wrote a simple controller class with request hitting to the "/". but always tomcat showing me "HTTP Status 404 – Not Found" error.

I looked many SO questions and those answers didn't work for me

  1. remove the tomcat installation and reconfigure it. when hitting localhost:8080/, I'm getting tomcat home page.
  2. checked the spelling of the code

web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>PSBankApp</display-name>
  <absolute-ordering />
  <servlet>
    <servlet-name>SpringController</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/bankAppContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>SpringController</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

bankAppContext.xml file

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.ps.springmvc.psbankApp.controllers"></context:component-scan>
        <mvc:annotation-driven></mvc:annotation-driven>

        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix" value="/WEB-INF/Views/" />
              <property name="suffix" value=".jsp" />
        </bean>
</beans>

AccountController.java file

package com.ps.springmvc.psbankapp.controllers;

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

@Controller
public class AccountController {

    @RequestMapping("/")
    public String showHomePage() {
        return "index";
    }
}

expected result should be: In the browser window "spring home page controller", which is a tag in the index.jsp file but the output is "HTTP Status 404 – Not Found" and description saying "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists."

following is my folder structure. 在此处输入图像描述

Any help would be appreciated.

Im not sure but package name in <context:component-scan base-package="com.ps.springmvc.psbankApp.controllers"> must be "com.ps.springmvc. psbankapp .controllers"?

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