I'm studing spring by YouTube guides) Resenty i tryied to start my own project, and got some issue. Resources from directory /static/** isn't provided to outer world - 404 error. /Static/ located in /src/main/resources/
I tried: add
spring.resources.static-locations=/js/,/css/
spring.mvc.view.prefix=/static/css/
spring.mvc.view.suffix=.css
in application.properties - no changes.
When i add
registry.addResourceHandler("/img/**").addResourceLocations("file:///"+uploadPath+"/");
to
public void addResourceHandlers(ResourceHandlerRegistry registry)
If i add .js file to /static/ or /static/js IDEA normally links the calls from my code to the scripts. But scripts don't work, when application started.
@Configuration
public class MvcConfig implements WebMvcConfigurer {
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
}}
WebSecurityConfig
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserService userService;
@Autowired
private PasswordEncoder passwordEncoder;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/users","/static/**").permitAll()
//.anyRequest().authenticated()
.anyRequest().permitAll()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.rememberMe()
.and()
.logout()
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws
Exception {
auth.userDetailsService(userService)
.passwordEncoder(passwordEncoder);
}
}
application.properties
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.url=jdbc:postgresql://localhost:5432/banking
spring.session.store-type=jdbc
spring.session.jdbc.initialize-schema=always
spring.datasource.username=postgres
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=false
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.freemarker.expose-request-attributes=true
project structure
├───main
│ ├───java
│ │ └───web
│ │ ├───configs
│ │ ├───controllers
│ │ ├───domain
│ │ ├───Repositories
│ │ ├───service
│ │ └───validator
│ └───resources
│ └───templates
│ ├───parts
│ └───static
│ ├───css
│ └───js
└───test
└───java
Please just move your directories from
src/main/resources/templates/static/
to
src/main/resources/static/
By default Spring Boot will serve static content from a folder called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext
Thanks