简体   繁体   English

thymeleaf正在基于其余URI加载静态资源

[英]thymeleaf is loading the static resources based on the rest URI

I'm using thymeleaf as a template engine to my spring boot project. 我在我的spring boot项目中使用thymeleaf作为模板引擎。

My directory structure is that: 我的目录结构是:

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

My controller is annotated with: 我的控制器带有以下注释:

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

And I have a method to get the users page: 我有一种获取用户页面的方法:

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

A method to create 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";
    }

And a method that when it's called, it add a user in the model (to fill a form): 还有一种方法,该方法在调用时会在模型中添加一个用户(以填写表格):

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

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

        return model;

    }

I'm using the same page, using includes to deal with that: 我使用的是同一页面,使用包含来处理此问题:

<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>

My CSS files been imported: 我的CSS文件已导入:

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

when I go to the user.html, it works as well. 当我转到user.html时,它也可以正常工作。 The problem, is when I go call the edit method that have a diferent URI. 问题是,当我去调用具有不同URI的edit方法时。

When I call the create the url seems like this: 当我调用create时,URL看起来像这样:

http://localhost:8080/administration/users

When I call the edit: 当我调用编辑时:

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

the import in both cases: 在两种情况下的导入:

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

But in the edit case, the browse console show me this: 但是在编辑情况下,浏览控制台向我显示了这一点:

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

You can use absolute instead of relative paths, for example /admin/css/template.css instead of ../../admin/css/template.css . 您可以使用绝对路径而不是相对路径,例如/admin/css/template.css而不是../../admin/css/template.css This may or may not work depending on your specific resource resolver configurations. 根据您特定的资源解析器配置,此方法可能有效或无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM