2
votes

I'm using Spring Boot and I want to know how exactly we mention the path to static content in my JSP files? I tried to make them in src/main/resources/static/css but it was not working, and in my JSP I called them by using:

<link href="<c:url value="/css/bootstrap.min.css" />" rel="stylesheet" type="text/css">

I have no special configuration in my SpringBoot Class just the call SpringApplication.run(...)

Thank you so much for the help!

2
does you config extends WebMvcAutoConfigurationAdapter ? - kuhajeyan
No my config is just a simple class annotated with @SpringBootApplication - marherbi

2 Answers

1
votes

you have to have configuration that extends WebMvcAutoConfigurationAdapter , it has registry implementation that has automatically scans for some default locations and adds them to classpath

  • /META-INF/resources/
  • /resources/
  • /static/
  • /public/

Just add,

@Configuration
@EnableWebMvc
@ComponentScan
public class ServerConfiguration extends WebMvcAutoConfiguration{
}   
0
votes

using springboot 1.5.6.RELEASE my folder structure looks like

~/repos/static-content-example/src > tree
.
├── main
│   ├── java
│   │   └── com
│   │       └── example
│   │           └── demo
│   │               ├── DemoApplication.java
│   │               └── MvcConfig.java
│   └── resources
│       ├── application.properties
│       ├── public
│       │   └── test.html
│       └── templates
└── test
    └── java
        └── com
            └── example
                └── demo
                    └── DemoApplicationTests.java

and when I start the server, I can browse to

  1. http://localhost:8080/test.html
  2. http://localhost:8080/public/test.html

anything in the folder "public" is accessible by default at your context root (#1 above). MvcConfig.java allows for #2. I always setup that alias so I can ignore security on any URL that starts with /public. In order to do that without the MvcConfig setup, you'd have to put a folder named public inside the public folder, which is just confusing.

I have no idea why spring doesn't do that by default....seems like it would clear up lots of confusion...