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?

5
  • Can you access yourhost/resources/js/my.js? Commented Sep 18, 2015 at 9:21
  • heck! it works in Internet explorer but does not work in chrome! Commented Sep 18, 2015 at 9:45
  • Do you disable cache? Commented Sep 18, 2015 at 9:48
  • I chek javascript in settings and it checled Commented Sep 18, 2015 at 9:52
  • can you paste your rea yourhost/resources/js/my.js Commented Sep 18, 2015 at 9:55

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Its the right answer. Did you move the resources folder, disabled caches, etc?
Yes i move "resource" folder
i add new structure in my quation
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.
0

Try:

<script src="${pageContext.request.contextPath}/resources/js/my.js"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/menu.css" />

Comments

0

Do you have taglibs available in your JSP?

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

Comments

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.

1 Comment

i create this structure but it not helped

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.