用Spring MVC链接jQuery文件 [英] link jQuery file with Spring MVC
问题描述
我在链接jQuery文件时遇到问题,已将.js文件放在
WebContent
--js
----- toggle.js
文件结构显示在......( http://i.imgur.com/oRzM0tP.png )
Web.xml文件
<?xml version =1.0encoding =UTF- 8\" >?;
< web-app xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns =http://java.sun.com/xml/ns/javaeexsi :schemaLocation =http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsdid =WebApp_IDversion =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>
但.js文件未链接,请给我一个指示,谢谢提前!!!
我认为你应该把 $ {pageContext.servletContext.contextPath}
你应该注意到Spring-dispatcher-servlet.xml中声明的名称(和版本)必须与/ js目录中的文件相同(jquery-1.6.2.js,jquery-1.11.1.js, jquery.js vv ..)
< script type =text / javascriptsrc =$ {pageContext.servletContext。的contextPath} /js/jquery-1.6.2.js>< /脚本>
并且如同所有人的建议,确保你有
< mvc:resources mapping =/ js / **location =/ js //> Spring-dispatcher-servlet.xml中的
<希望它有效
I have an issue in linking jQuery file, have placed the .js file under
WebContent
--js
-----toggle.js
file structure is shown in... (http://i.imgur.com/oRzM0tP.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!!!
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
这篇关于用Spring MVC链接jQuery文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!