Yooouuri Yooouuri - 3 months ago 18
CSS Question

Spring MVC won't load static content

I am new to Spring and i want to load some css and js into a view.

The static content is now in

src/main/resources/static/...
but when i navigate to
localhost:8080/css/test.css
it gives me a 404 error..

I don't use any xml configuration files.

My project structure:

project structure

Update: Application class

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);
}
}


Update: 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")
}


Some controller (just testing stuff):

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";
}
}

Answer

Your structure is correct. Don't forget to make gradle clean task before run bootRun.