0

I have an issue in linking jQuery file, have placed the .js file under

WebContent

--js

-----toggle.js

file structure is shown in... (https://i.sstatic.net/4lZpj.png)

Web.xml file

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>FirstSpringMVCProject</display-name>

      <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
         <servlet-class> 
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
      </servlet>

     <servlet-mapping>
       <servlet-name>spring-dispatcher</servlet-name>
          <url-pattern>/</url-pattern>
     </servlet-mapping>

</web-app>

Spring-dispatcher-servlet.xml

   <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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.gontuseries.studentadmissioncontroller" />

    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/js/**" location="/js/" />


       <mvc:annotation-driven/>

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

      </bean>

but .js file is not linked up, please give me a directions, thanks in advance!!!

6
  • can you please show your web.xml file also Commented Oct 13, 2014 at 14:54
  • Sorry @BijuKunjummen for the late reply, have edited the question, thanks for your help... Commented Oct 13, 2014 at 15:49
  • 1
    I think you just need to add a <mvc:default-servlet-handler /> to your spring-dispatcher-servlet.xml file. This will essentially delegate handling of the static content to the container. Additionally remove the <mvc:resources entries, that will also work but with potentially things in your classpath, which it is not in this specific instance. Commented Oct 13, 2014 at 16:07
  • Thanks @BijuKunjummen, still it doesn't link with .js file, I can't figure it out, where I am going wrong. Thanks for your directions - Commented Oct 13, 2014 at 16:17
  • Any errors, anything on the console, try to get the log level to DEBUG to see what could be going wrong. Spring should show enough information at this log level Commented Oct 13, 2014 at 16:21

2 Answers 2

1

I think you should put ${pageContext.servletContext.contextPath} and you should notice the name (and version ) declare in Spring-dispatcher-servlet.xml must same with the file in /js directory (jquery-1.6.2.js,jquery-1.11.1.js, jquery.js v.v...)

<script type="text/javascript" src="${pageContext.servletContext.contextPath}/js/jquery-1.6.2.js"></script>

and as everyone suggest, make sure you have

<mvc:resources mapping="/js/**" location="/js/" />

in Spring-dispatcher-servlet.xml

hope it work

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

1 Comment

Unbelievable! it worked, Thank you so much @Tung Vo,
0

Its not a very much spring way, but I could link a javascript file as below

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

Please check if it works for you also.!

Comments

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.