Spring WebJars 教程展示了如何在 Spring Web 应用程序中使用 WebJar。

Spring是一个流行的Java应用程序框架,用于创建企业 应用。

Webjars

WebJars是打包的客户端Web库(例如jQuery或Semantic UI) 到 JAR(Java 存档)文件中。WebJars 自动化前端工作 库和资源。

Spring WebJar 示例

在下面的示例中,我们使用 Semantic-UI WebJar。语义UI是一种流行的 CSS 框架。

pom.xml
src
├───main
│   ├───java
│   │   └───com
│   │       └───zetcode
│   │           ├───config
│   │           │       MyWebInitializer.java
│   │           │       WebConfig.java
│   │           └───controller
│   │                   MyController.java
│   └───resources
│       │   logback.xml
│       └───templates
│               index.html
└───test└───java    

这是项目结构。

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>com.zetcode</groupId><artifactId>WebJarEx</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><spring-version>5.1.4.RELEASE</spring-version><thymeleaf-version>3.0.11.RELEASE</thymeleaf-version></properties><dependencies><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.4.0</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version><scope>provided</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.webjars</groupId><artifactId>Semantic-UI</artifactId><version>2.4.1</version></dependency><dependency><groupId>org.webjars</groupId><artifactId>webjars-locator</artifactId><version>0.34</version></dependency><dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf-spring5</artifactId><version>${thymeleaf-version}</version></dependency><dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf</artifactId><version>${thymeleaf-version}</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>3.3.2</version></plugin></plugins></build>
</project>

pom.xml我们有项目依赖项。

<dependency><groupId>org.webjars</groupId><artifactId>Semantic-UI</artifactId><version>2.4.1</version>
</dependency>

我们使用Semantic-UI WebJar。

<dependency><groupId>org.webjars</groupId><artifactId>webjars-locator</artifactId><version>0.34</version>
</dependency>

webjars-locator允许我们直接不带版本号引用资产,自动检测引用的资产版本。

resources/logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration><logger name="org.springframework" level="ERROR"/><logger name="com.zetcode" level="INFO"/><appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender"><encoder><Pattern>%d{HH:mm:ss.SSS} %blue(%-5level) %magenta(%logger{36}) - %msg %n</Pattern></encoder></appender><root><level value="INFO" /><appender-ref ref="consoleAppender" /></root>
</configuration>

这是配置logback.xml

com/zetcode/config/MyWebInitializer.java
package com.zetcode.config;import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.FrameworkServlet;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;@Configuration
public class MyWebInitializer extendsAbstractAnnotationConfigDispatcherServletInitializer {@Overrideprotected Class<?>[] getRootConfigClasses() {return null;}@Overrideprotected Class<?>[] getServletConfigClasses() {return new Class[]{WebConfig.class};}@Overrideprotected String[] getServletMappings() {return new String[]{"/"};}
}

MyWebInitializer初始化 Spring Web 应用程序。它包含一个 配置类:。WebConfig

com/zetcode/config/WebConfig.java
package com.zetcode.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.zetcode"})
public class WebConfig implements WebMvcConfigurer {@Autowiredprivate ApplicationContext applicationContext;@Beanpublic SpringResourceTemplateResolver templateResolver() {var templateResolver = new SpringResourceTemplateResolver();templateResolver.setApplicationContext(applicationContext);templateResolver.setPrefix("classpath:templates/");templateResolver.setSuffix(".html");return templateResolver;}@Beanpublic SpringTemplateEngine templateEngine() {var templateEngine = new SpringTemplateEngine();templateEngine.setTemplateResolver(templateResolver());templateEngine.setEnableSpringELCompiler(true);return templateEngine;}@Beanpublic ViewResolver viewResolver() {var resolver = new ThymeleafViewResolver();var registry = new ViewResolverRegistry(null, applicationContext);resolver.setTemplateEngine(templateEngine());registry.viewResolver(resolver);return resolver;}@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/").resourceChain(false);}@Overridepublic void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {configurer.enable();}
}

配置百里香叶模板引擎,告诉 Spring 在哪里查找 WebJars 并启用转发到默认 servlet 用于处理静态资源。WebConfig

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/").resourceChain(false);
}

我们将通过路径引用WebJars。 必须为版本无关而调用该方法 网络罐子。/webjars/resourceChain

com/zetcode/controller/MyController.java
package com.zetcode.controller;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;import java.util.List;@Controller
public class MyController {@GetMapping(value = "/")public String home(Model model) {var words = List.of("wood", "star", "cloud", "water","river", "spring");model.addAttribute("words", words);return "index";}
}  

MyController包含主页的一个路由。我们发送一些 数据到模板。数据将以 HTML 表格的形式呈现,该表格将 使用语义 UI 设置样式。

resources/templates/index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Home page</title><link rel="stylesheet" th:href="@{/webjars/Semantic-UI/semantic.css}">
</head>
<body><section class="ui container"><h2>English words</h2><table class="ui striped celled table"><thead><tr><th>Index</th><th>Word</th></tr></thead><tbody><tr th:each="word : ${words}"><td th:text="${wordStat.index + 1}">Index</td><td th:text="${word}">A word</td></tr></tbody></table></section></body>
</html>

这是主页。

<link rel="stylesheet" th:href="@{/webjars/Semantic-UI/semantic.css}">

我们链接到来自WebJar的文件。semantic.css

<table class="ui striped celled table">

CSS 类来自 Semantic-UI 库。

在本教程中,我们创建了一个语义UIWebJar来设置HTML样式。 桌子。

Spring WebJars 教程相关推荐

  1. Spring Boot 教程(三): Spring Boot 整合Mybatis

    教程简介 本项目内容为Spring Boot教程样例.目的是通过学习本系列教程,读者可以从0到1掌握spring boot的知识,并且可以运用到项目中.如您觉得该项目对您有用,欢迎点击收藏和点赞按钮, ...

  2. 【译】Spring官方教程:使用STS的入门指南

    原文:Working a Getting Started guide with STS 译者:hanbin 校对:Mr.lzc 这个指南引导您使用 Spring Tool Suite (STS) 去构 ...

  3. 2017.3.31 spring mvc教程(六)转发、重定向、ajax请求

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...

  4. 最新 Spring 系列教程,都在这了

    转载自  最新 Spring 系列教程,都在这了 Spring Boot 系列 什么是 Spring Boot? 公司不用 Spring Boot,果断离职了! 告诉你,Spring Boot 真是个 ...

  5. spring mvc教程_Spring MVC教程

    spring mvc教程 1.简介 作为企业Java开发人员,这项工作的主要重点之一是开发Web应用程序. 对于Web应用程序,后果还包括许多挑战. 具体来说,其中一些是状态管理,工作流和验证. HT ...

  6. Spring Cloud教程– Spring Cloud Config Server简介

    问题 SpringBoot在通过属性或YAML文件外部化配置属性方面提供了很大的灵活性. 我们还可以使用特定于配置文件的配置文件(例如application.properties , applicat ...

  7. spring mvc 教程_Spring MVC开发–快速教程

    spring mvc 教程 这是我们的JCG合作伙伴之一,来自Manoj的有关使用Spring开发Web应用程序的简短教程, 网址为" The Khangaonkar Report &quo ...

  8. Spring Batch教程–最终指南

    这是Spring批处理教程,它是Spring框架的一部分. Spring Batch提供了可重用的功能,这些功能对于处理大量记录至关重要,包括日志记录/跟踪,事务管理,作业处理统计信息,作业重新启动, ...

  9. “Spring入门”教程系列

    大家好, 我很高兴向您介绍"Spring入门"教程系列! 这是一系列文章,最初由我们的JCG合作伙伴 Michal Vrtiak在vrtoonjava博客上撰写 . 本系列中将逐步 ...

最新文章

  1. 完美世界第二题:模拟读数字
  2. Crontab和sudo中无法使用TensorFlow ImportError libcublas.so.9.0
  3. SrpingCloud 之SrpingCloud config分布式配置中心
  4. 第四范式获信通院尖峰开源项目及开源人物双料大奖
  5. CSS中设置border属性为0与none的区别
  6. android上传图片文件至c 服务器,Android 史上最优雅的实现文件上传、下载及进度的监听...
  7. [零基础学JAVA]Java SE应用部分-35.JAVA类集之四
  8. 东莞华勤通讯软件测试怎么样,【社招】华勤通讯NBD测试验证部急聘岗位-东莞...
  9. CPU GPU设计工作原理《转》
  10. ccproxy8.0破解版
  11. 苹果笔记本计算机内存不足怎么办,macbook内存不够用怎么加_苹果电脑增加内存的具体方法...
  12. STL 堆 鱼塘钓鱼
  13. VirtualBox安装增强工具时:Unable to install guest additions: unknown filesystem type 'iso9660'
  14. java 随机发牌_java实现扑克牌发牌器
  15. 学习笔记(01):Web前端与HTML5移动开发系列一:HTML篇-06,HTML基本构成和语法
  16. sap服务器迁移性能问题,专家详解SAP数据迁移的六个方法
  17. 明光市机器人_明光市情侣酒店客房语音智能控制系统厂家
  18. el-table-column嵌套el-autocomplete使用
  19. 【DVB】DVB-T系统的参数和搜台介绍
  20. 2022年底总结(被阿里捞的第1、2、3...次)

热门文章

  1. 计算机组成原理笔记|01计算机系统概论
  2. 剖析非同质化代币ERC721-全面解析ERC721标准
  3. BIM+GIS技术为工程数字化转型提供了新的契机
  4. 2007年全国硕士研究生入学统一考试(英语一)
  5. 使用 MVVMLight 消息通知
  6. xadmin安装与使用
  7. [ 案例源码 ] 利用php开发apicloud 前台加后台源码
  8. caffe-ristretto:定点方案
  9. 测绘坐标系统与坐标转换
  10. 机器学习学习笔记-多项式中的过拟合,泛化能力等