0

[see updates at the bottom] I'm trying to launch a Spring web app via Tomcat, but when I enter the url http://localhost:8080/demo-mvc/ in the browser, I get the 404 error and a message "The requested resource [/demo-mvc/] is not available". At first I was trying to deploy the app via NetBeans and it said that deployment was successful, but at browser launch I got the 404 error. I checked the Tomcat/webapps directory and didn't find the app directory there somewhy, so I decided to manually pick the app directory from NetBeans' projects folder and move it to webapps (and rename it to demo-mvc for brevity), still there's an error. The content of app's config files is the following:

web.xml

<?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_3_1.xsd"
    id="WebApp_ID" version="3.1">

    <display-name>spring-mvc-demo</display-name>

    <!-- Spring MVC Configs -->

    <!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
    <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/spring-mvc-demo-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
</web-app>

spring-mvc-demo-servlet.xml

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

    <!-- Step 3: Add support for component scanning -->
    <context:component-scan base-package="com.luv2code.springdemo" />

    <!-- Step 4: Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven/>

    <!-- Step 5: Define Spring MVC view resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

There's also a source file HomeController.java

package com.luv2code.springdemo.mvc;

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

@Controller
public class HomeController {
    @RequestMapping("/")
    public String showPage()
    {
        return "main-menu";
    }
}

and a main-menu.JSP file in the WEB-INF\view location.

UPD: I tried moving the war to webapps, the corresponding folder appeared automatically. Now the requested resource message is gone, but the 404 is still there.

UPD: There are actually two controllers in the app and when I add /hello/showForm to the url, the appropriate page shows up, so there's something wrong with the main page only. The second controller code is: HelloWorldController.java

@Controller
@RequestMapping("/hello")
public class HelloWorldController {
    @RequestMapping("/showForm")
    public String showForm()
    {
        return "helloworld-form";
    }
    @RequestMapping("/processForm")
    public String processForm()
    {
        return "helloworld";
    }
    ...
}

And there is helloworld-form.jsp in the view folder.

4
  • If you're learning Spring, discard whatever tutorial this is; essentially all of this setup is obsolete. Use Spring Boot with Thymeleaf; it's simpler and more reliable, and you can generate a standalone project in one minute, no Tomcat required. Commented Aug 21, 2021 at 18:38
  • Yes, I'm learning Spring, but I'm not willing to discard this tutorial, because I'm certain it is useful. Also I am curious about why this is happening. Commented Aug 21, 2021 at 18:58
  • You are not using Tomcat 10 by any chance? Commented Aug 21, 2021 at 20:02
  • Nope. It's 9.0.52. Commented Aug 21, 2021 at 20:24

1 Answer 1

1

The issue is resolved. There was a default index.html file in the webapp directory created by NetBeans. When I deleted that file, the request mapping worked and my index page showed up.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.