org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI

15,927

You have the following

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>

mapping some resources to the Spring servlet but you don't have any controller handler methods to serve them up. You should remove the above servlet-mappings and put your css resources in /resources/css and js resources in /resources/js. Spring will know to get them automatically from there because of the <mvc:resources> element.

Make sure to also change the hard-coded location of your scripts and css in html files.

Also, on this line

<link href='<c:url value="/resources/css/ExpenseDetailsStyles.css" />' ... />

use double quotes ". You might also want to refactor to this

The <c:url> tag will put the value into the variable myvar which will then be available in your <link> tag.

Also you need this line at the top of your jsp to tell it you need jstl tags:

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
Share:
15,927
Freakyuser
Author by

Freakyuser

I have an experience of more than 3 years in Java platform and a beginner in Springs and Hibernate usage. I mainly practice Swimming, Running, Cycling and Triathlon. The maximum I have done: in Triathlon is Ironman (3.86 km swim, 180.25 km cycle &amp; 42.2 km run); in Swimming is 4.5 km; in Cycling is 530 km; in Running is 50 km;

Updated on June 13, 2022

Comments

  • Freakyuser
    Freakyuser almost 2 years

    I referred to many posts but still I was unable to solve this problem.

    enter image description here

    I have tried putting this css folder in WEB-INF and within views folder as well.
    But still the styles are not reflected in the view page.

    Here is my servlet-context.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            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">
    
        <resources mapping="/resources/**" location="/resources/" />
    
        <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <beans:property name="prefix" value="/WEB-INF/views/" />
            <beans:property name="suffix" value=".jsp" />
        </beans:bean>
    
        <context:component-scan base-package="ctc.event.control" />
    
    </beans:beans>
    

    This is my web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
        <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/root-context.xml</param-value>
        </context-param>
    
        <!-- Creates the Spring Container shared by all Servlets and Filters -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <!-- Processes application requests -->
        <servlet>
            <servlet-name>appServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>appServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
    </web-app>
    

    This is my style sheet ExpenseDetailsStyles.css

    #ItemDiv1_Table1 {
        border: 1px solid black;
    }
    
    #ItemDiv1Table2 {
        width: 20%;
        float: right;
        font-size:30px;
    }
    

    and my view page.

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <%@ include file="TagIncludes.jsp" %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Expense Details</title>
    <link href='<c:url value="/resources/css/ExpenseDetailsStyles.css" />' rel="stylesheet" type="text/css" media="screen" />
    <script type='text/javascript' src="../js/jquery-1.9.0.js"></script>
    <script type="text/javascript"></script>
    </head>
    <body>
        <form:form name="ExpenseDetails" method="Post"
            action="ExpenseDetailsForm.html">
            <div id="ItemDiv1">
                <table>
                    <tr>
                        <td>
                            <table id="ItemDiv1_Table1">
                                <tr>
                                    <td>Item</td>
                                    <td><input type="text" name="Item1" id="Item1" required /></td>
                                </tr>
                                <tr>
                                    <td>Expense in Rs</td>
                                    <td><input type="text" name="ItemValue1" id="ItemValue1" ></td>
                                </tr>
                            </table>
                        </td>
                        <td>
                            <table id="ItemDiv1_Table2">
                                <tr>
                                    <td>Edit</td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </div>
        </form:form>
    </body>
    </html>
    

    Here is the controller

    @Controller
    public class ExpenseFormController {
    
        @RequestMapping(value = "/ExpenseDetailsForm.html", method = RequestMethod.GET)
        public String openForm() {
            return "ExpenseDetailsForm";
    
        }
    
        @RequestMapping(value = "/ExpenseDetailsForm.html", method = RequestMethod.POST)
        public String submitForm() {
            return null;
    
        }
    }
    

    stacktrace is not an error but a warning.

    WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/Calculator/<c:url value="css/ExpenseDetailsStyles.css" />] in DispatcherServlet with name 'appServlet'

    I think there is some problem with my uri in the view page and I tried changing it in many different ways. Can anyone please help me correct the uri and reflect the styles on the view page?
    Please comment if any other information pertaining this, is needed.

  • Freakyuser
    Freakyuser about 11 years
    Thank you for the answer, I already have it in my servlet-context.xml. I have posted it above.
  • Freakyuser
    Freakyuser about 11 years
    I have removed the servlet-mapping for css and js. I have this, '<c:url value="css/ExpenseDetailsStyles.css" />' in the html. Is this correct?
  • Freakyuser
    Freakyuser about 11 years
    By the way, the resources we are talking about is the one in src/main/webapp and not the one src/main/, am I right?
  • Sotirios Delimanolis
    Sotirios Delimanolis about 11 years
    I believe it should be <c:url value="/resources/css/Expens...> and in src/main/webapp
  • Freakyuser
    Freakyuser about 11 years
    It is still not working, even after using src/main/webapp and c:url value="/resources/css/Expens.... I think there is something else missing in the resources mapping="/resources/**" location="/resources/" /
  • Sotirios Delimanolis
    Sotirios Delimanolis about 11 years
    @Freakyuser update your question with the modified web.xml, servlet-context.xml, and jsp
  • Sotirios Delimanolis
    Sotirios Delimanolis about 11 years
    This looks like the correct setup to me. Try cleaning your project, rebuilding, and running it again. And show us what new error occurs if it does.
  • Freakyuser
    Freakyuser about 11 years