1

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";
    }
}
yooouuri
  • 2,578
  • 10
  • 32
  • 54
  • update your application class here – kuhajeyan Sep 07 '16 at 08:18
  • please post code of your `Application` class – jahra Sep 07 '16 at 08:18
  • `/src/main/resources` isn't used for keeping static content. Depending on what type of a configuration you are using, there is a separate directory for storing web-static content. In most cases `/src/main/web/WEB-INF...`. Please have a look at [this](https://docs.spring.io/docs/Spring-MVC-step-by-step/part1.html). – px06 Sep 07 '16 at 08:21
  • But the templates are loaded in the ```sources/template``` folder. Is there a way to load the css/js files from the static folder? – yooouuri Sep 07 '16 at 08:23
  • 1
    http://stackoverflow.com/questions/29018892/how-add-static-web-content-in-spring-boot will be helpful – jahra Sep 07 '16 at 08:25
  • I checked your structure and it works pretty good. Do you run your application with debug mode or just run compiled jar file? – jahra Sep 07 '16 at 08:34
  • Use something like https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-war/src/main – Sully Sep 07 '16 at 08:49
  • @jahra in the terminal i just run ```./gradlew bootRun``` to start the website. – yooouuri Sep 07 '16 at 08:56
  • 1
    Can you post your `build.gradle` and your controller code here? Maybe you have some dependencies that override default settings – jahra Sep 07 '16 at 09:01
  • 1
    @jahra Added the build.gradle and controller – yooouuri Sep 07 '16 at 09:19
  • 1
    I reproduced all your code and it works as it should. So I don't know how to help you anymore – jahra Sep 07 '16 at 09:30
  • 1
    @jahra are you able to view the test.css? – yooouuri Sep 07 '16 at 09:31
  • 1
    yes, my static resources are available. Do your template files work properly? – jahra Sep 07 '16 at 09:37
  • 1
    @jahra Yes the templates are working fine. The path is: ```/src/main/resources/static/css``` and yours? – yooouuri Sep 07 '16 at 09:39
  • 1
    Yes. The same path as you have. Also try to make `gradle -> clean` and `gradle -> build` before `bootRun` – jahra Sep 07 '16 at 09:45
  • 1
    I'm still trying to find my notes on this, but I'm sure I had a similar issue and it turned out to be because I'd not included thymeleaf in all the right places. Will update if I find it. – Graham Nicol Sep 07 '16 at 09:48
  • 1
    @jahra all the time i did a build, not a clean build. So now its working, thanks you all!! – yooouuri Sep 07 '16 at 09:49
  • 1
    Make `clean` everytime you update your project. Because some resources will not be attached to build directory. You are welcome! – jahra Sep 07 '16 at 09:51

1 Answers1

1

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

jahra
  • 1,173
  • 1
  • 16
  • 41