0

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. enter image description here

Any help would be appreciated.

Nadee
  • 342
  • 1
  • 6
  • 18
  • SpringController ??? Maybe AccountController ? – Valentyn Anzhurov Nov 06 '19 at 16:40
  • @ValentynAnzhurov `SpringController` is the name of their `DispatcherServlet`. – Sotirios Delimanolis Nov 06 '19 at 16:44
  • @ValentynAnzhurov I tried using AccountController as well , but the same 404 gave – Nadee Nov 06 '19 at 16:45
  • 1
    you probably need `/` of the application, not the server. Something like `http://localhost:8080/PSBankApp/` to trigger servlet that intercepts `/` of that application. – Jimmy Nov 06 '19 at 17:04
  • @Nadee Downgrade your Servlet Version to 3.0 – Anish B. Nov 06 '19 at 17:17
  • Your package structure looks a tad strange, are you using maven to manage your project? You mention that you received the tomcat homepage, even after removing all the default webapps, how are you deploying/running tomcat? – hooknc Nov 06 '19 at 17:17
  • @hooknc, I'm not using maven to manage dependencies. I installed tomcat version 9.0.27 in my C drive. I configure path to the tomcat directory in server tab of eclipse. then right click the project and gave run on server. (if that what're asking about running tomcat") – Nadee Nov 06 '19 at 17:20
  • try adding org.springframework.web.context.ContextLoaderListener – AchillesVan Nov 06 '19 at 17:29
  • @Nadee, thank you for the reply/answers. I am sure that you have looked at this, but just incase, have you properly setup tomcat in eclipse? [Configuration of the Eclipse Workspace](https://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.stardust.docs.wst%2Fhtml%2Fwst-integration%2Fconfiguration.html) – hooknc Nov 06 '19 at 17:30
  • To follow up with @Arish B.'s question, what version of tomcat are you trying to run? – hooknc Nov 06 '19 at 17:32
  • @hooknc I'm using latest version of apache tomcat. (9.0.27). I'm pretty sure I have setup my tomcat properly. I have already worked on couple of dynamic web projects. but first time I'm getting this. can't figure out where. – Nadee Nov 06 '19 at 17:34
  • You could upload your app on github and share it here – AchillesVan Nov 06 '19 at 17:35
  • 1
    Try to your context.xml. feels like it doesn't load the annotated classes – Manuel Nov 06 '19 at 17:51
  • @ManuelPolacek still the same :( tried everthing in the comment section. – Nadee Nov 06 '19 at 17:59

1 Answers1

0

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

tunapq
  • 354
  • 3
  • 13