Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC - include static files / javascript , css

I have created MVC application.

I want to include js or css file into jsp.

My static files ar under:

- webapp
        -js/jquery.js
        -WEB-INF|
                |
                 - jsp/*.jsp

My code to include jquery is:

<script type="text/javascript" src="<c:url value="js/jquery.js" />"></script>

and i cant load the js file into view.

I see the logs with info:

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/pool/js/jquery.js] in DispatcherServlet with name 'appServlet'

what means, that MVC try to map url to js file.

I think that there is something with my configuration, but i don't know what.

my web.xml is:

<?xml version="1.0" encoding="UTF-8"?>

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>

  <filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

like image 240
Ilkar Avatar asked Dec 20 '11 22:12

Ilkar


People also ask

Where does Spring MVC put CSS files?

Put your css/js files in folder src/main/webapp/resources . Don't put them in WEB-INF or src/main/resources .


1 Answers

add this to you confing and modify the location according to your need.

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

see this How to handle static content in Spring MVC?

like image 91
Josef Procházka Avatar answered Nov 01 '22 06:11

Josef Procházka