首页 文章

使用@PathVariable时如何使用spring boot和thymeleaf导入首页中的静态资源

提问于
浏览
1

当我刚才使用 @RequestMapping 时,我可以很好地获得html渲染

@RequestMapping("/demo")
    public String news() {
        return "demo";
    }

但是当我添加param @PathVariable("docid") 时,它丢失了所有静态资源 .

@RequestMapping("/single/{docid}")
public String single( @PathVariable("docid") String docid) {
    return "demo";
}

在浏览器控制台中,它说 failed to load resource: the server responded with a ststus of 404() .

我使用localhost:8080/single/123来启动页面,它将 demo.html 没有任何静态资源,但是当我使用localhost:8080/demo时,它运行良好 .

我将import语句更改为:src = "@{/js/my.js}",它仍然从 localhost:8080/single/js/my.js 搜索静态资源,我该怎么办?

Ps:我用Thymeleaf作为模板 .

2 回答

  • 0

    使用URL作为http://host:port/single/1001,并且没有理由获得404.确保在路径(URL)中使用docid值 .

  • 0

    我解决了它,我将 th:fragment 中的所有import语句更改为绝对路径 .

相关问题