简体   繁体   English

如何在Java Web应用程序中从JSP引用JavaScript库?

[英]How can I reference a javascript library from a JSP in a java web application?

I am trying to create a table with sortable columns using a javascript library provided by this website . 我正在尝试使用此网站提供的javascript库创建具有可排序列的表格。 The web application is being created in Java and the table is created but not with the CSS style. 正在用Java创建Web应用程序,并创建表,但不使用CSS样式。 I am 100% new to html and javascript and just started trying to learn things today. 我是html和javascript的100%新手,今天才开始尝试学习。

I have placed the library in the same source folder as the JSP and also at the root of the project but both times the table is created in standard html format without the script formatting. 我将库与JSP放在相同的源文件夹中,也放在项目的根目录下,但是两次都以标准html格式创建表,而没有脚本格式。 How should I reference a Javascript library from a JSP in Java Web Application? 如何在Java Web应用程序中从JSP引用Javascript库?

I have made some progress but I still cannot get it to work properly. 我已经取得了一些进展,但是仍然无法正常工作。 This is my WelcomePage.jsp file: 这是我的WelcomePage.jsp文件:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%response.setHeader("Refresh", "5");%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>title</title>
        <script type="text/javascript" src="sorttable.js"></script>
    </head>
    <body>
        <p><a href=url target=\"_blank\">Start</a></p>
        <table class="sorttable">
            <tr>
                <td>text </td>
                <td>text </td>
                <td>text </td>
                <td>text </td>
                <td>text </td>
            </tr>
        </table>
    </body>
</html>

I have tried placing it into the META-INF, WEB-INF, and the root folders none of them seem to work correctly. 我尝试将其放入META-INF,WEB-INF和根文件夹中,但它们似乎都无法正常工作。 I have also attempted to add it to the web.xml file but nothing seems to work. 我也尝试将其添加到web.xml文件中,但似乎没有任何效果。

文件层次

Some things I have already tried 我已经尝试过的一些东西

Can not include javascript file from WEB-INF directory in JSP. 无法在JSP中包含来自WEB-INF目录的javascript文件。

how to reference a java .class file from a JSP page? 如何从JSP页面引用Java .class文件?

Put it at the root of your application, or under a /scripts folder there. 将其放在应用程序的根目录下,或/ scripts文件夹下。 WEB-INF and META-INF are not accessible from outside. 不能从外部访问WEB-INF和META-INF。

If it is not accessible, then check the logs, try opening the js file manually in the browser, and check with Firebug (or similar tool) if there aren't javascript errors. 如果无法访问,请检查日志,尝试在浏览器中手动打开js文件,然后使用Firebug(或类似工具)检查是否没有javascript错误。

Then, if everything is ok, I suppose you need to invoke some function and pass the table id. 然后,如果一切正常,我想您需要调用一些函数并传递表ID。

由于您将要创建网页,并且可能需要更多的js库,因此为什么不在Web页中创建一个仅用于js的新文件夹呢?

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

相关问题 如何从Java JSP JavaScript打开Excel? - How can I open an excel from a java jsp javascript? 如何使用JSP和Javascript将Applets应用程序转换为Web应用程序? - How can I convert an Applets application into an Web application using JSP and Javascript? 我如何在Web java应用程序(jsp)中加载Bootstrap的css和js? - How I load css and js of Bootstrap in a web java application (jsp)? Java到JSP-如何将Java应用程序集成到JSP网页中? - Java to JSP - How do I integrate a Java application into a JSP web page? 如何在MVC Web应用程序(Servlet和Jsp)中处理异常? - How can I handle exception in a MVC web application (Servlet and Jsp)? 如何从 JAVA 中的其他 JSP 重定向到 JSP - how can I redirect to a JSP from other JSP in JAVA 如何在 java web 应用程序的 jsp 中实现 if 语句? - How can i implement an if statement in a jsp in java web app? 如何通过Java连接URL执行JSP的JavaScript? - How can i execute javascript of jsp throught java connection url? 如何在Java Web应用程序中创建可以从jsp中的表达式语言调用的函数? - How can I create my own functions that I can call from Expression Language in jsp in a Java web app? 来自jsp中web.xml的Java Web应用程序全局参数 - Java web application global parameters from web.xml in jsp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM