gpt4 book ai didi

spring-boot - 在 Spring Boot 中集成 JQuery

转载 作者:行者123 更新时间:2023-12-04 17:52:57 26 4
gpt4 key购买 nike

我在将 JQuery 集成到 Spring 引导项目(Spring 新手)时遇到了问题。我试图按照文档进行操作,但它仍然不起作用。我正在使用 Java/Spring + Thymeleaf。文档说我必须执行以下操作:

  1. The WebJar needs to be a dependency of your application
  2. The WebJar needs to be in your application's running CLASSPATH
  3. Your container, web framework, or application needs to serve static assets from Jar files

引用:http://www.webjars.org/documentation#springboot

这是我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId>
<artifactId>gs-securing-web</artifactId>
<version>0.1.0</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- tag::security[] -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- end::security[] -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>

<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

然后我还将资源处理程序添加到我的 Config 类中,它扩展了 WebMvcConfigurerAdapter 类。我添加了以下方法:

        @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}

然后在我的 HTML 文件中有以下几行:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
<script type="text/javascript">
jQuery(document).ready(function($) {
alert("JQuery works");
});
</script>
</head>
<body>
<h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>
<form th:action="@{/logout}" method="post">
<input type="submit" value="Sign Out"/>
</form>
<script type="text/javascript" src="webjars/jquery/3.2.0/jquery.min.js"></script>
</body>
</html>

但是 JQuery 仍然没有加载。它说它是未定义的。谁能告诉我哪里出了问题?

最佳答案

好的,我发现了问题。关于配置的一切都是正确的,但我必须添加 <script type="text/javascript" src="webjars/jquery/3.2.0/jquery.min.js"></script>在文档准备功能之前。谁能解释为什么会这样?

关于spring-boot - 在 Spring Boot 中集成 JQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43057387/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com