Frank Serkland
Frank Serkland

Reputation: 273

Spring framework -- no mapping found for a JavaScript file

I'm getting what appears to me to be a strange error when I load my JSP. Spring is a Java framework, so why would I need a mapping for a JavaScript file? The JavaScript in my page is not working.

org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/ghs1986/js/javascript.js] in DispatcherServlet with name 'ghs1986'

For what it's worth, here is my applicationContext.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/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-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
    <mvc:annotation-driven />

    <mvc:resources location="WEB-INF/pages/images/" mapping="/images/**" />

    <mvc:view-controller path="/" view-name="home" />

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

This is how I'm trying to access the JavaScript in my JSPs.

<script type="text/javascript" src="js/javascript.js"></script>

And here is my web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<web-app version="4.0"
        xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
          http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd">
    <display-name>Granada High School Class of 1986</display-name>

    <servlet>
        <servlet-name>ghs1986</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <welcome-file-list>
        <welcome-file>home.jsp</welcome-file>
    </welcome-file-list>
</web-app>

Upvotes: 2

Views: 1988

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201419

In much the same way as you have a mapping to allow the spring controller to know about images with

<mvc:resources location="WEB-INF/pages/images/" mapping="/images/**" />

you need to add a similar mapping to let it know about your javascript files. Something like,

<mvc:resources location="WEB-INF/pages/js/" mapping="/js/**" />

The issue is the same in both cases, the front end controller of Spring doesn't know how to find your js files without that.

Upvotes: 2

Related Questions