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

How to introduce jquery in webjars

WBOY
Release: 2023-05-18 19:45:36
Original
543 people have browsed it

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>
    Copy after login

    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">
    Copy after login

    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">
    Copy after login
  3. Using jQuery in a JavaScript file:

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

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

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

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template