【问题标题】:Spring MVC won't load static contentSpring MVC 不会加载静态内容
【发布时间】:2017-01-14 19:40:12
【问题描述】:

我是 Spring 新手,我想将一些 css 和 js 加载到视图中。

静态内容现在位于 src/main/resources/static/...,但是当我导航到 localhost:8080/css/test.css 时,它给了我一个 404 错误..

我不使用任何 xml 配置文件。

我的项目结构:

更新:应用程序类

package com.exstodigital.photofactory;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

更新:build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
    }
}

group 'PTS4'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-devtools")
    testCompile("junit:junit")
}

一些控制器(只是测试东西):

package com.exstodigital.photofactory;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
//@Controller
public class MainController {

    //@ResponseBody
    @RequestMapping("/greeting/{name}/{hoi}")
    public String greeting(@PathVariable("name") String name, @PathVariable("hoi") String hoi) {
        //model.addAttribute("message", "BERICHT");

        return name;
    }

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

【问题讨论】:

  • 您可以在这里发布您的build.gradle 和您的控制器代码吗?也许你有一些覆盖默认设置的依赖项
  • @jahra 添加了 build.gradle 和控制器
  • 我复制了你的所有代码,它可以正常工作。所以我不知道如何帮助你了
  • @jahra 你能查看 test.css 吗?

标签: java css spring spring-mvc static-content


【解决方案1】:

你的结构是正确的。不要忘记在运行bootRun 之前创建 gradle clean 任务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-12
    • 2017-04-05
    • 1970-01-01
    • 2017-07-19
    • 2021-09-09
    • 2017-03-25
    • 2014-10-15
    • 2019-03-04
    相关资源
    最近更新 更多