Results 1 to 5 of 5

Thread: <mvc:resources> doesn't work with javascript files

  1. #1
    Join Date
    Jun 2011
    Posts
    20

    Default <mvc:resources> doesn't work with javascript files

    Hello, I'm new with Spring and I'm trying to use my javascript files in my jsp.
    Now.. my dispatcher-servlet is:
    Code:
    .....   
    <context:component-scan base-package="bosmaci.org.controller"/>
     
        <!-- Configures the @Controller programming model -->
        <mvc:annotation-driven/>
        
        <!-- the mvc resources tag does the magic -->
     	<mvc:resources mapping="/resources/**" location="/resources/" />	
     
        <!-- Un modo comodo per mappare le URL direttamente sulle JSP, senza dover creare un controller -->
        <mvc:view-controller path="/about.html" view-name="about" />
     
        <!-- View Resolver for JSPs -->
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="requestContextAttribute" value="rc"/>
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <property name="prefix" value="/WEB-INF/pages/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    ...
    my web is:
    Code:
      <display-name>Spring project</display-name>
      
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
          classpath*:/applicationContext.xml
        </param-value>
      </context-param>
      
      <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
      </filter>
      
      <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    
      <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      
      <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.html</url-pattern>
      </servlet-mapping>
    
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
      </welcome-file-list>
    In my jsp :
    Code:
    ....
    <body>
    	<p>query requested</p>
    	<c:out value="${query}" default="sucamilla"  />
    	<img src='<c:url value="/resources/images/git-cheat-sheet-large.png" />' />
    	<script type="text/javascript" src='<c:url value="/resources/js/commons.js" />'>
    	getAlert();
    		</script>
    </body>
    ....
    It is able to find the image but is not able to find javascript file...
    why?
    Any help will be appreciate..

  2. #2
    Join Date
    Oct 2005
    Location
    Mobile, AL
    Posts
    345

    Default

    That looks like it should work. Have you tried the following?

    From your browser, enter the full url to access your image. So for example, type in the image url which may be somehting like this : http://localhost/testApp/resources/i...heet-large.png

    You should see the image displayed. Now try the js file

    From your browser, enter the full url to access your image. So for example, type in the image url which may be somehting like this : http://localhost/testApp/resources/js/commons.js

    Do you see the javascript listed? If not then something is wrong with your configuration or your path structure.

  3. #3
    Join Date
    Jun 2011
    Posts
    20

    Default

    Thanks for your help...
    yes..I tried and I can see my javascript file from browser...

  4. #4
    Join Date
    Oct 2005
    Location
    Mobile, AL
    Posts
    345

    Default

    Hang on, I think I see your problem. I don't think you can load a javascript file and put actual javascript syntax within the same javascript tag. Try this:

    Code:
    ....
    <body>
        <p>query requested</p>
        <c:out value="${query}" default="sucamilla"  />
        <img src='<c:url value="/resources/images/git-cheat-sheet-large.png" />' />
        <script type="text/javascript" src='<c:url value="/resources/js/commons.js" />'></script>
        
        <script type="text/javascript">
            getAlert();
        </script>
        
    </body>
    ....

  5. #5
    Join Date
    Jun 2011
    Posts
    20

    Default

    Thank you very much...it works now...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •