Home >Web Front-end >Front-end Q&A >How to introduce jquery in webjars

How to introduce jquery in webjars

WBOY
WBOYOriginal
2023-05-18 19:45:36540browse

WebJars is a way of encapsulating Java language libraries. It encapsulates client libraries (such as jQuery) into a Jar package. This allows developers to manage and introduce client libraries through Maven without the need to manually download and Import external files. The client libraries in WebJars can be found on the classpath of the web application.

Introducing jQuery into a web application can be achieved by following the steps below:

  1. Add the following dependencies in the pom.xml file:

    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>3.5.1</version>
    </dependency>

    Here we take the introduction of jQuery version 3.5.1 as an example. You can modify the version number as needed.

  2. Add the following tag in the HTML file of the web application:

    <link rel="stylesheet" href="/webjars/jquery/{version}/dist/jquery.min.js">

    Note that {version} is replaced with the imported jQuery version, For example:

    <link rel="stylesheet" href="/webjars/jquery/3.5.1/dist/jquery.min.js">
  3. Using jQuery in a JavaScript file:

    $(document).ready(function() {
        console.log("jQuery is working!");
    });

    Alternatively you can use the short alias $ instead of jQuery, for example:

    $(function() {
        console.log("jQuery is working!");
    });

After completing the above steps, the web application can use the jQuery library in WebJars. If you are using the Spring Boot framework, the webjars-locator library has been introduced by default in Maven, so no additional configuration is required.

If you need to introduce client libraries from other WebJars, you can also follow the above steps. It should be noted that the href attribute in the tag needs to be modified according to the imported library. For example, to introduce the Bootstrap library, you can write like this:

<link rel="stylesheet" href="/webjars/bootstrap/{version}/css/bootstrap.min.css">
<script src="/webjars/bootstrap/{version}/js/bootstrap.min.js"></script>

At the same time, you also need to add the import dependency in pom.xml:

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>bootstrap</artifactId>
    <version>5.1.0</version>
</dependency>

The above is how to use WebJars to introduce jQuery in the Maven environment. If Use in other environments may require additional configuration. But in general, using WebJars in web applications can easily manage and maintain client libraries and avoid the trouble of manual downloading and importing.

The above is the detailed content of How to introduce jquery in webjars. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn