[英]Include jquery in spring mvc app
I am trying to include jquery in a spring mvc app but am having some trouble (I am new to spring mvc). 我正在尝试将jquery包含在spring mvc应用程序中,但是遇到了一些麻烦(我是spring mvc的新手)。 I created a folder called js under the webapp folder, but am not sure what to include my view to include the file.
我在webapp文件夹下创建了一个名为js的文件夹,但不确定要包含什么内容才能包含该文件。
I've tried: 我试过了:
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-2.0.3.min.js"></script>
however, this creates a path that cannot be resolved since I have no controller for /js path. 但是,这会创建一个无法解析的路径,因为我没有用于/ js路径的控制器。
My question is how is this usually done, is a controller created that returns the jquery-2.0.3.min.js file? 我的问题是这通常是如何完成的,是否创建了一个返回jquery-2.0.3.min.js文件的控制器?
Although you could link to a CDN for common libraries such as jquery or bootstrap, most applications eventually will need some custom CSS, JS, or images. 尽管您可以链接到CDN以获得通用库,例如jquery或bootstrap,但是大多数应用程序最终都将需要一些自定义CSS,JS或图像。 Instead of writing a controller, you should be serving the resources via Spring MVC's ResourceServlet.
您应该通过Spring MVC的ResourceServlet提供资源,而不是编写控制器。 You can add the following to your webmvc-config.xml (or default-servlet.xml or whatever spring mvc configuration file you have):
您可以将以下内容添加到webmvc-config.xml(或default-servlet.xml或任何具有的spring mvc配置文件)中:
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
<mvc:resources location="/" mapping="/resources/**"/>
Then, you could update your view to include "resources" in the path, such as: 然后,您可以更新视图以在路径中包括“资源”,例如:
<spring:url value="/resources/js/jquery-2.0.3.min.js" var="jquery_js_url" />
<script src="${jquery_js_url}" type="text/javascript"><!-- /required for FF3 and Opera --></script>
For more information, see the following: 有关更多信息,请参见以下内容:
Hope that helps... 希望有帮助...
I would recommend just linking to a CDN (such as Google) -- as it enables jquery to be loaded from a user's cache, rather than your servers: 我建议仅链接到CDN(例如Google)-因为它使jquery可以从用户的缓存而不是服务器中加载:
https://developers.google.com/speed/libraries/devguide#jquery https://developers.google.com/speed/libraries/devguide#jquery
I have this line in my JSPs, and it works. 我的JSP中有这一行,并且行得通。 We do not use a CDN - it is hosted locally in the javascript folder.
我们不使用CDN-它在javascript文件夹中本地托管。 That folder hangs under the webapps parent.
该文件夹挂在webapps父文件夹下。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<script src='<c:url value="/javascript/jquery.js"/>'></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.