【问题标题】:thymeleaf is loading the static resources based on the rest URIthymeleaf 正在基于 rest URI 加载静态资源
【发布时间】:2015-10-16 23:38:25
【问题描述】:

我正在使用 thymeleaf 作为我的 Spring Boot 项目的模板引擎。

我的目录结构是这样的:

src/main/
|- java
    | - UserAdministrationController.java
|- resources
    | - static
        | - admin
             | - css
                 | - template.css
             | - js
                 | - template.js
    | - templates
        |- incs
            |- inc_form_create_user.html
        |- users.html

我的控制器带有注释:

@Controller
@RequestMapping("/administration/users")

我有一种方法可以获取用户页面:

@RequestMapping("")
    public ModelAndView getUsersPage() {
        return new ModelAndView("admin/users");
    }

一种创建用户的方法:

@RequestMapping(value = "/create", method = RequestMethod.POST)
    public String handleUserCreateForm(@Valid @ModelAttribute("form") UserDTO form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return getUsersPage().getViewName();
        }
        try {
            userService.create(form);
        } catch (DataIntegrityViolationException e) {
            bindingResult.reject("email.exists", "Email already exists");
            return getUsersPage().getViewName();
        }
        return "redirect:/administration/users";
    }

还有一个方法,当它被调用时,它会在模型​​中添加一个用户(以填写表格):

@RequestMapping("/{id}/edit")
    public ModelAndView getEditPage(@PathVariable("id") long id) {
        ModelAndView model = new ModelAndView("admin/users");

        model.addObject("userToEdit", userService.getUserById(id));

        return model;

    }

我正在使用相同的页面,使用包含来处理:

<div th:if="${userToEdit == null}" class="panel panel-default" th:include="admin/incs/inc_form_create_user :: createUserForm" ></div>
<div th:if="${userToEdit != null}" class="panel panel-default" th:include="admin/incs/inc_form_create_user :: editUserForm" ></div>

我的 CSS 文件已导入:

<link th:href="@{../../admin/css/template.css}" rel="stylesheet" />

当我转到 user.html 时,它也可以正常工作。问题是当我调用具有不同 URI 的编辑方法时。

当我调用 create 时,url 看起来像这样:

http://localhost:8080/administration/users

当我调用编辑时:

http://localhost:8080/administration/users/10/edit

两种情况下的导入:

../../admin/css/template.css

但在编辑情况下,浏览控制台会显示:

Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8080/administration/admin/css/template.css

【问题讨论】:

    标签: html css spring-boot thymeleaf


    【解决方案1】:

    您可以使用绝对路径代替相对路径,例如/admin/css/template.css 代替../../admin/css/template.css。这可能会也可能不会起作用,具体取决于您的特定资源解析器配置。

    【讨论】:

      猜你喜欢
      • 2018-09-11
      • 2016-08-17
      • 1970-01-01
      • 2023-02-15
      • 2017-07-26
      • 2015-03-08
      • 2015-01-05
      • 2018-06-02
      • 1970-01-01
      相关资源
      最近更新 更多