在 Spring 引导中集成 JQuery
Integrating JQuery in Spring Boot
我在将 JQuery 集成到 Spring 引导项目时遇到问题(Spring 的新功能)。我试图按照文档进行操作,但它仍然不起作用。我正在使用 Java/Spring + Thymeleaf。文档说我必须执行以下操作:
- The WebJar needs to be a dependency of your application
- The WebJar needs to be in your application's running CLASSPATH
- 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>
然后我还将资源处理程序添加到我的配置 class 中,它扩展了 WebMvcConfigurerAdapter class。我添加了以下方法:
@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>
。有人可以解释为什么会这样吗?
我在将 JQuery 集成到 Spring 引导项目时遇到问题(Spring 的新功能)。我试图按照文档进行操作,但它仍然不起作用。我正在使用 Java/Spring + Thymeleaf。文档说我必须执行以下操作:
- The WebJar needs to be a dependency of your application
- The WebJar needs to be in your application's running CLASSPATH
- 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>
然后我还将资源处理程序添加到我的配置 class 中,它扩展了 WebMvcConfigurerAdapter class。我添加了以下方法:
@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>
。有人可以解释为什么会这样吗?