【发布时间】: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