首页 > 解决方案 > @PathVariable 不加载资源 [CSS & JS] - Thymeleaf

问题描述

嗨,我是 Spring Boot 的新手,我正在开发用于编辑产品的功能,在我的类 ProductController 中,我使用 @PathVariable 接收产品 ID,加载时 form-edit.html 不加载 css 和js 文件。我尝试配置 MVC 添加 ResourcesHandler 但不适用于此视图...

MVCConnfig.java

WebMvcConfigurer.super.addResourceHandlers(registry);
    registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/");
    registry
    .addResourceHandler("/resources/**")
    .addResourceLocations("/resources/");

产品控制器.java

@RequestMapping(value = "/form-edit/{id}")
public String editar(@PathVariable(value = "id") Integer id, Map<String, Object> model, 
RedirectAttributes flash) {

    Producto producto = null;

    if (id > 0) {
        producto = productoService.findOne(id);
        if (producto == null) {
            flash.addFlashAttribute("error", "El ID del producto no existe en la BBDD!");
            return "redirect:/panel";
        }
    } else {
        flash.addFlashAttribute("error", "El ID del producto no puede ser cero!");
        return "redirect:/panel";
    }
    ..Here load some lists for catalogs in select option..
    return "panel-form";
}

应用程序属性

spring.web.resources.chain.strategy.content.enabled=true
spring.web.resources.chain.strategy.content.paths=/resources/**
spring.web.resources.chain.strategy.fixed.enabled=true
spring.web.resources.chain.strategy.fixed.version=v12

从视图 Panel.html 加载标题和头部:

表单-edit.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="panel :: head"></head>
<body>
  <header th:replace="panel :: header"></header>
     <div class="container py-4">
       <div class="card bg-dark text-white">
          <div class="card-header" th:text="${titulo}"></div
            <form th:action="@{/form}" th:object="${producto}" 
                method="post"
                enctype="multipart/form-data">

标签: javaspringthymeleaf

解决方案


推荐阅读