1

I am new to spring mvc and tomcat. I have developed a demo spring mvc project and trying to deploy it on tomcat 9 through eclipse. Server starts successfully but when i try to access the url from browser i get 404 with below error message on screen. :

Message The requested resource [/spring-mvc-demo/] is not available

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Below are my code details :

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>

<absolute-ordering />

<!-- 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>/spring-mvc-demo</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="main.webapp" />

<!-- 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>

HomeController.java

package main.webapp.springdemo.controller;

import javax.annotation.PostConstruct;

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

@Controller
 public class HomeController {

@PostConstruct
public void init() {
    System.out.println("HomeController bean getting intiated");
}

@RequestMapping("/")
public String getMainMenu() {
    return "main-menu";
}
}

I tried running this app on tomcat 9 through eclipse as well as manually but in both the scenarios i got same error.

ANKIT SRIVASTAVA
  • 133
  • 4
  • 14
  • Can you reconfirm your servlet mapping? – VedantK Aug 24 '21 at 22:51
  • @VeKe servlet-mapping is correct as provided in the question. Do you see any issues with it? – ANKIT SRIVASTAVA Aug 24 '21 at 22:53
  • Can you go through this answer once https://stackoverflow.com/a/11731512/1878022 – VedantK Aug 24 '21 at 22:58
  • @VeKe i went through the answer you shared and noticed my war file does not have the classes folder under WEB-INF. Not sure though why my war file doesnt have class files – ANKIT SRIVASTAVA Aug 24 '21 at 23:10
  • *I am new to spring mvc and tomcat.* Then discard your obsolete tutorial and use Spring Boot. You can completely discard your XML configuration and your Tomcat setup. [Spring Initializr](https://start.spring.io) will auto-generate a runnable project skeleton for you. (And I strongly recommend Thymeleaf instead of JSP; it fixes a lot of the core problems with it and is still pretty familiar.) – chrylis -cautiouslyoptimistic- Aug 24 '21 at 23:38

1 Answers1

1

Assuming the application deploys without errors (check your logs), the URI path you are using to access it is almost certainly wrong. In a servlet environment the URI path decomposes as:

<context-path><servlet-path><path-info>

where:

  • <context-path> is the prefix to your application. In the Eclipse server configuration page this is called just "path" and defaults to /<project-name>,
  • <servlet-path> is configured through the <servlet-mapping> element in your web.xml deployment descriptor,
  • <path-info> is the part usually used by Spring to perform its internal routing (unless alwaysUseFullPath is set on the HandlerMapping).

Therefore (theoretically) you should try accessing:

http://localhost:8080/projectName/spring-mvc-demo/

There is however another problem: your servlet mapping is an exact mapping, that does not match anything else beyond /projectName/spring-mvc-demo. You should replace it with a prefix mapping (see this question for an overview of servlet mappings):

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/spring-mvc-demo/*</url-pattern>
</servlet-mapping>

If you want to shorten your URL, the DispatcherServlet is usually mapped as default servlet /. Be aware not to use the catchall prefix mapping /*, which would override the mapping of the JSP servlet.

Remark: There are some cases when Spring does not use the part after <servlet-path> for its routing, e.g. when you use an exact or extension mapping, the whole path after <context-path> is used.

Therefore if you use:

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/spring-mvc-demo</url-pattern>
</servlet-mapping>

you should specify:

@RequestMapping("/spring-mvc-demo")
public String getMainMenu() {
   ...
}

and use the URL http://localhost:8080/appName/spring-mvc-demo.

Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43
  • I tried the url http://localhost:8080/spring-mvc-demo/spring-mvc-demo and could see blow error in server logs : WARNING: No mapping for GET /spring-mvc-demo/spring-mvc-demo/ . I am using spring-mvc-demo as my context-path since it is the name of my project. – ANKIT SRIVASTAVA Aug 25 '21 at 18:50
  • Do you have a trailing slash? Since your requested mapping is `/` (with the servlet mapping in my answer) your should use `http://localhost:8080/spring-mvc-demo/spring-mvc-demo/` (with a trailing `/`). I forgot to mention: when you use **exact** servlet mappings, Spring behaves as if `alwaysUseFullPath` were enabled. – Piotr P. Karwasz Aug 25 '21 at 19:51