1

I can not include javaScript and css fiels to my JSP page. I tried this and this but not helped.

My JSP:

  <html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
        <script src="../resources/js/my.js"></script>
        <link rel="stylesheet" href="../resources/css/menu.css" />
...

and mapping:

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

    <context:component-scan base-package="com.springapp.mvc"/>
    <mvc:annotation-driven />
    <mvc:resources mapping="/resources/**" location="resources" cache-period="31556926"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

and ctructure:

WEB-INF
 -pages
   -my.jsp
 -resources
   -js
     -my.js
   -css
     -menu.css
 -mvc-dispatcher-servlet.xml
 -web.xml

new structure not helped

webapp -resources -js -my.js -css -menu.css -WEB-INF -pages -my.jsp -mvc-dispatcher-servlet.xml -web.xml

What i do wrong?

Community
  • 1
  • 1
Pavel
  • 251
  • 1
  • 4
  • 18

4 Answers4

1

Move your resources folder outside your WEB-INF and make your like below.

<script src="/resources/js/my.js"></script>
<link rel="stylesheet" href="/resources/css/menu.css" />

You can't refer to the resources in your WEB-INF folder by relative path, you have mentioned.

Abhiram mishra
  • 1,597
  • 2
  • 14
  • 34
  • Its the right answer. Did you move the resources folder, disabled caches, etc? – Stefan Sep 18 '15 at 09:20
  • Yes i move "resource" folder – Pavel Sep 18 '15 at 09:22
  • i add new structure in my quation – Pavel Sep 18 '15 at 09:25
  • Looking at your above comment as it works on your "Can you access yourhost/resources/js/my.js?" , with having the resources moved out of your WEB-INF folder. Make sure you do a ctrl+f5 on your browser to fetch the recent change on the jsp page. Hoping your don't have any other sort of caching installed. – Abhiram mishra Sep 18 '15 at 09:57
0

Try:

<script src="${pageContext.request.contextPath}/resources/js/my.js"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/menu.css" />
Amila
  • 5,195
  • 1
  • 27
  • 46
0

Your structure should be:

webapp
  -WEB-INF
    -mvc-dispatcher-servlet.xml
    -web.xml
    -pages
      -my.jsp
  -resources
    -js
     -my.js
    -css
     -menu.css

so that the static resources are not under WEB-INF. The <mvc:resources> tag tells spring not to serve these URLs at all, leave them up to your servlet container (Tomcat etc) to serve. But Tomcat et al will not serve content directly from the WEB-INF directory.

EDIT

<mvc:resources mapping="/resources/**"
               location="/"
               cache-period="31556926" />

Assuming your project uses the standard directory layout, put your Javascript under: src/main/webapp/js/my.js and then access them with the relative URL /resources/js/my.js. Hope that helps.

NickJ
  • 9,380
  • 9
  • 51
  • 74
0

Do you have taglibs available in your JSP?

If so consider something like: <script src="<c:url value="/js/my.js"/>"></script>

DaFoot
  • 1,475
  • 8
  • 29
  • 58