简体   繁体   中英

Can't find a way to use jQuery in my JSP, help me please

I'm actually working on a website using JSP and Servlet.

I'm not a pro so I try to do my best and I wanted to implement a calendar that would show up on one of my webpages.

I checked the web and found this : http://arshaw.com/fullcalendar/

It seemed promising so even though I'm a noob in JS I downloaded the package and tried to use it.

But that didn't work.

I've tried a lot of things and I'm going to die if I can't find a solution to this.

The problem comes from jQuery, which doesn't work. At all.

Here's my code :

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Planning</title>
        <link rel="stylesheet" type="text/css" href="./styleERDF.css" media="screen">
        <link rel="stylesheet" type="text/css" href="./styleImpression.css" media="print">
        <link rel="stylesheet" type="text/css" href="C:\Users\Maxime\Documents\NetBeansProjects\SiteWebERDF\web\calendar\fullcalendar\fullcalendar.css" media="all">
        <link rel="stylesheet" type="text/css" href="./styleMenu.css" media="screen">
        <script type="text/javascript" src="C:\Users\Maxime\Documents\NetBeansProjects\SiteWebERDF\web\calendar\lib\jquery.min.js"></script>
        <script type="text/javascript" src="C:\Users\Maxime\Documents\NetBeansProjects\SiteWebERDF\web\calendar\fullcalendar\fullcalendar.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                alert("PLEASE WORK");                
            });
        </script>
    </head>
    <body>
        <div id="general">

            <div id="entete" >
                <h1><img src="Images\ERDF.png" width="Auto" height="Auto" alt="ERDF"></h1>
            </div>
            <div class="noImpr" id="menu">
                <%@ include file="../menu.html"%>                
            </div>
        </div>
    </body>
</html>

The fact is : nothing works. I mean, HTML does. The webpage loads and show the first image and the menu.html, but the script doesn't do anything. Isn't it supposed to show me a message ?

You can see that I used the full path for the jQuery file, because I really was desperated. I'm working on two computers, this one who has Internet access, and an other one, on which I program, who doesn't have Internet access, so I can't use web url.

EDIT:

Current project structure (current jsp in folder "pages":

http://i.stack.imgur.com/9bVBb.png

The problem is that you're loading the files from local disk. Load them directly from the web application resources. Usually, you do this using ${request.contextPath} that already contains http://yourIp/yourApplicationName :

<script type="text/javascript" src="${request.contextPath}/calendar/lib/jquery.min.js"></script>

Rewriting all your sources to this style:

<link rel="stylesheet" type="text/css" href="${request.contextPath}/styleERDF.css" media="screen">
<link rel="stylesheet" type="text/css" href="${request.contextPath}/styleImpression.css" media="print">
<link rel="stylesheet" type="text/css" href="${request.contextPath}/calendar/fullcalendar/fullcalendar.css" media="all">
<link rel="stylesheet" type="text/css" href="${request.contextPath}/styleMenu.css" media="screen">
<script type="text/javascript" src="${request.contextPath}/calendar/lib/jquery.min.js"></script>
<script type="text/javascript" src="${request.contextPath}/calendar/fullcalendar/fullcalendar.min.js"></script>

<!-- ... -->

<img src="${request.contextPath}/Images/ERDF.png" width="Auto" height="Auto" alt="ERDF">

Note that example above will work only on JSP pages. If working in HTML page, then you have to call the resources statically from the current path:

<link rel="stylesheet" type="text/css" href="./styleERDF.css" media="screen">
<script type="text/javascript" src="./calendar/lib/jquery.min.js"></script>

Before you cram HTML into a JSP, get it working as a plain HTML file. I think the single backslashes are incorrect, try using single forward slashes. Also, as someone else pointed out, you will have better luck using a relative path. You can serve HTML from your Java Web App server, just put it in the "WebContent" folder or whatever you named it in your project.

使用CDN

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

Change from this:

<script type="text/javascript" src="C:\Users\Maxime\Documents\NetBeansProjects\SiteWebERDF\web\calendar\lib\jquery.min.js"></script>
<script type="text/javascript" src="C:\Users\Maxime\Documents\NetBeansProjects\SiteWebERDF\web\calendar\fullcalendar\fullcalendar.min.js"></script>

To This

<script type="text/javascript" src="../calendar/lib/jquery.min.js"></script>
<script type="text/javascript" src="../calendar/fullcalendar/fullcalendar.min.js"></script>

I am assuming that the "SiteWebERDF" directory on your machine is the one that has JSP in it. More info can be found here on relative paths: http://www.w3.org/TR/WD-html40-970917/htmlweb.html#h-5.1.2

+1 for Jason P. But if you MUST load full file paths...

Instead of:

<script type="text/javascript" src="C:\Users\Maxime\Documents\NetBeansProjects\SiteWebERDF\web\calendar\lib\jquery.min.js
</script>

Use this (presuming you are on Windows:

<script type="text/javascript" src="file:///C:/Users/Maxime/Documents/NetBeansProjects/SiteWebERDF/web/calendar/lib/jquery.min.js">
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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