简体   繁体   English

如何在JSP中设置javascript路径

[英]How can I set the javascript path in my JSP

I am working on SpringMVC3.2, JSP and Tomcat using Maven build. 我正在使用Maven构建来开发SpringMVC3.2,JSP和Tomcat。 My web application structure is shown like below. 我的Web应用程序结构如下所示。

tomcat
  |-- webapps
    |-- MyApp
      |-- css
      |-- js
        |-- jquery.js
      |-- images
      |-- META-INF
      |-- WEB-INF
        |-- classes
        |-- lib
        |-- src
        |-- web.xml
        |-- pages
             |-- index.jsp

I set the JavaScript path in the index.jsp like following: 我在index.jsp中设置JavaScript路径,如下所示:

<script src="../js/jquery.js"></script>

But it could not load the Javascript file. 但是它无法加载Javascript文件。 I changed path and moved the file here and there but everything did not work. 我更改了路径,并在此四处移动了文件,但一切都无法正常进行。 What am I missing? 我想念什么? Please help me. 请帮我。

Thanks in advance. 提前致谢。

EDIT Actually, I found the answer through different community. 编辑实际上,我是通过其他社区找到答案的。

<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
    <script src="${contextPath}/js/jquery-1.9.1.js"></script>

</head>
<body>
</body>
</html>

This is not a beautiful way but it works fine for me and if you want different solution then see my answer below. 这不是一个好方法,但是对我来说很好,如果您想使用其他解决方案,请在下面查看我的答案。

tomcat
  |-- webapps
    |-- YourApplicatipName
      |-- css
      |-- js
        |-- jquery.js
      |-- images
      |-- META-INF
      |-- WEB-INF
        |-- classes
        |-- lib
        |-- src
        |-- web.xml

th js folder should be in your root folder only js文件夹应仅位于您的根文件夹中

now your given path would work 现在您给定的路径将起作用

web.xml web.xml

<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
<scripting-invalid>true</scripting-invalid>
<include-prelude>/WEB-INF/common/commonDefinition.jspf</include-prelude>
</jsp-property-group>
</jsp-config>

commonDefinition.jspf commonDefinition.jspf

<%@ page language="java" contentType="text/html; charset=UTF-8" %><%@ 
taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ 
taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %><%@ 
taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %><%@ 
taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM