【问题标题】:CSS and Java Script not rendering 404 error spring-bootCSS 和 Javascript 不呈现 404 错误 spring-boot
【发布时间】:2017-01-06 12:46:43
【问题描述】:

这是我正在使用的路径的 sn-p:

<link rel="stylesheet" type="text/templates.css" href="../templates/css/superfish.css" th:href="@{../templates/css/superfish.css}"/>
<link rel="stylesheet" type="text/templates.css" href="../templates/css/style.css" th:href="@{../templates/css/style.css}"/>
<link rel="stylesheet" type="text/templates.css" href="../templates/css/nivo-slider.css" th:href="@{../templates/css/nivo-slider.css}"/>
<link rel="stylesheet" type="text/templates.css" href="../templates/css/jquery.qtip.min.css" th:href="@{../templates/css/jquery.qtip.min.css}"/>
<link rel="stylesheet" type="text/templates.css" href="../templates/css/jquery-ui.css" th:href="@{../templates/css/jquery-ui.css}"/>
<link rel="stylesheet" type="text/templates.css" href="../templates/css/jquery.fancybox.css" th:href="@{../templates/css/jquery.fancybox.css}"/>
<link rel="stylesheet" type="text/templates.css" href="../templates/css/jquery.fancybox-buttons.css" th:href="@{../templates/css/jquery.fancybox-buttons.css}"/>

控制器:

 @RequestMapping(value="/", method= RequestMethod.GET)
    public String contactForm(@Valid @ModelAttribute("contact") Contact contact, BindingResult bindingResult,
                              HttpServletRequest request, Model model, Device device) throws IOException {

        System.out.println("Inside controller-----------------");

主要方法:

@SpringBootApplication
@ComponentScan("com.stidham")
public class StidhamFinancialApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(StidhamFinancialApplication.class);
    }

    public static void main(String[] args) {

        SpringApplication.run(StidhamFinancialApplication.class, args

        );
    }
}

只有 html 呈现,没有 js 或 css。我尝试了几种方法,我目前的路径看起来是正确的,甚至当我输入路径时,我的 IDE 也会自动填充。

在war文件中,它们也位于正确的位置。

mvn clean install 构建正确,它们没有依赖问题。这似乎只是一个在部署时不起作用的简单路径问题。

---------------更新1---------------

<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <meta name="format-detection" content="telephone=no"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
    <link rel="stylesheet" type="text/css" href="/css/superfish.css" />
    <link rel="stylesheet" type="text/css" href="/css/style.css" />
    <link rel="stylesheet" type="text/css" href="/css/nivo-slider.css" />
    <link rel="stylesheet" type="text/css" href="/css/jquery.qtip.min.css" />
    <link rel="stylesheet" type="text/css" href="/css/jquery-ui.css" />
    <link rel="stylesheet" type="text/css" href="/css/jquery.fancybox.css" />
    <link rel="stylesheet" type="text/css" href="/css/jquery.fancybox-buttons.css"/>

新路径

js 和 css 仍然是 404:

【问题讨论】:

    标签: javascript css spring-boot


    【解决方案1】:

    “templates”文件夹只保留给 thymeleaf 处理 html。 JavaScript 和 CSS 应位于“静态”文件夹中。此文件夹直接映射到 http 的根 URL。

    示例:文件位于 ... /resources/static/css/superfish.css

    <link rel="stylesheet" href="/css/superfish.css" />
    

    【讨论】:

    • 您忘记创建名为“static”的目录
    猜你喜欢
    • 2021-11-02
    • 2018-12-24
    • 2017-08-26
    • 2021-05-04
    • 1970-01-01
    • 2017-11-02
    • 2017-09-06
    • 1970-01-01
    • 2018-06-26
    相关资源
    最近更新 更多