Getting "WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI ..." when trying to setup spring servlet

18,273

Solution 1

You seem to be missing the <mvc:annotation-driven />

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

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />

    <context:component-scan base-package="org.activiti.explorer.controller" />

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

note that I've removed the version from the xsd files, this means that it will use the schema from your jar files (and there will be a validation error in case of incompatibilty)

after @Nikolay's comment, I've also noticed an error in your mapping (note that you still need the annotation-driven element), you should either change the mapping in your controller to

@RequestMapping("/hello.jsp")

and access it via

/safesite/hello.jsp

OR, more common, change the servlet mapping to

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

and access as Nikolay said so /safesite/hello

Solution 2

I had a similar issue before which was very confusing, after a series of tests, I found it's the url-pattern for the DispatcherServlet. Be cautious when you use the asterisk for wildcard match which might lead to unexpected behavior, and it's just safe to start from root "/" or your custom servlet context path "/foo/"

Try the following.

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

Share:
18,273
OneTwo
Author by

OneTwo

Updated on July 23, 2022

Comments

  • OneTwo
    OneTwo almost 2 years

    I am trying to setup a Spring MVC project. I have added a dispatcher servlet, a jsp and setup the web.xml file. But I keep getting

    WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/safesite/WEB-INF/jsp/hello.jsp] in DispatcherServlet with name 'HelloWeb'

    Here's my web.xml

    ...
    <context-param>
        <description>Vaadin production mode</description>
        <param-name>productionMode</param-name>
        <param-value>true</param-value>
    </context-param>
    
    <!-- To load the Spring context -->
    <listener>
        <listener-class>org.activiti.explorer.servlet.WebConfigurer</listener-class>
    </listener>
    
    <!-- To allow session-scoped beans in Spring -->
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    
    <filter>
        <filter-name>UIFilter</filter-name>
        <filter-class>org.activiti.explorer.filter.ExplorerFilter</filter-class>
    </filter>
    
    <filter>
        <filter-name>JSONPFilter</filter-name>
        <filter-class>org.activiti.explorer.servlet.JsonpCallbackFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>UIFilter</filter-name>
        <url-pattern>/o/*</url-pattern>
    </filter-mapping>
    
    <filter-mapping>
        <filter-name>JSONPFilter</filter-name>
        <url-pattern>/service/*</url-pattern>
    </filter-mapping>
    
    <servlet>
        <servlet-name>Vaadin Application Servlet</servlet-name>
        <servlet-class>org.activiti.explorer.servlet.ExplorerApplicationServlet</servlet-class>
        <init-param>
            <param-name>widgetset</param-name>
            <param-value>org.activiti.explorer.CustomWidgetset</param-value>
        </init-param>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>Vaadin Application Servlet</servlet-name>
        <url-pattern>/ui/*</url-pattern>
    </servlet-mapping>
    
    <servlet-mapping>
        <servlet-name>Vaadin Application Servlet</servlet-name>
        <url-pattern>/VAADIN/*</url-pattern>
    </servlet-mapping>
    
    <servlet>
        <servlet-name>HelloWeb</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>HelloWeb</servlet-name>
        <url-pattern>*.jsp</url-pattern>
    </servlet-mapping>
    
    <!-- Session timeout on one day -->
    <session-config>
        <session-timeout>480</session-timeout>
    </session-config>
    

    And here is my HelloWeb-servlet.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    <context:component-scan base-package="org.activiti.explorer.controller" />
    
    <bean id="viewResolver"
              class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
          value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    

    My HelloController

     package org.activiti.explorer.controller;
    
     import org.springframework.stereotype.Controller;
     import org.springframework.web.bind.annotation.RequestMapping;
     import org.springframework.web.servlet.ModelAndView;
    
     /**
      *
      * @author Fionn
      */
     @Controller
     public class HelloController {
    
    @RequestMapping("/hello")
    public ModelAndView helloWorld() {
    
             String message = "<br><div align='center'>"
                     + "<h3>********** Hello World, Spring MVC Tutorial</h3>This message is comming from CrunchifyHelloWorld.java **********<br><br>";
             return new ModelAndView("hello", "message", message);
         }
    }
    

    And my hello.jsp

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"     %>
    <%@page trimDirectiveWhitespaces="true" %>
    <!DOCTYPE html>
    <html>
    <head>
    <title>Hello Spring MVC</title>
    </head>
    <body>
    

    I can't figure this one out so any help would be greatly appreciated.

  • OneTwo
    OneTwo about 9 years
    replace my HelloWeb-servlet with this?
  • Master Slave
    Master Slave about 9 years
    not replace, merge, you need to add the annotation element, and the you need to include the mvc namespace, but you also need your two existing elements (component-scan and view resolver) I've just copy pasted an excerpt, that has everything you need, you should be able to find your way in merging easialy
  • OneTwo
    OneTwo about 9 years
    Thanks for the help, but I still get the same warning.
  • Master Slave
    Master Slave about 9 years
    no problem, noticed another thing and editied in the response, hope it'll help
  • OneTwo
    OneTwo about 9 years
    Could you explain why this fixed it? Should my url not have .jsp at the end of it?