Reputation: 130
I recently needed to move all my resources from a folder 3DShop/src/main/webapp/resources/ to 3DShop/src/main/resources/ After the transfer, css styles on the pages stopped working and images also stopped displaying.
I changed the paths in the spring settings in the addResourceHandlers method, and the path in my html template when I included the link. However, the styles and pictures still don't work. I think I'm not specifying the addresses correctly in the addResourceHandlers method. How to prescribe the correct path, please explain.
My spring config class:
package ru.shop.three_d_print.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.web.servlet.config.annotation.*;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import javax.sql.DataSource;
@Configuration
@ComponentScan("ru.shop.three_d_print")
@EnableWebMvc
public class SpringConfig implements WebMvcConfigurer
{
private final ApplicationContext applicationContext;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/WEB-INF/classes/static/**").addResourceLocations("/WEB-INF/classes/static/");
}
@Autowired
public SpringConfig(ApplicationContext applicationContext)
{
this.applicationContext = applicationContext;
}
@Bean
public SpringResourceTemplateResolver templateResolver()
{
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(applicationContext);
templateResolver.setPrefix("/WEB-INF/classes/templates/");
templateResolver.setSuffix(".html");
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine()
{
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry)
{
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
registry.viewResolver(resolver);
}
@Bean
public JdbcTemplate jdbcTemplate() { return new JdbcTemplate(dataSource()); }
@Bean
public DataSource dataSource()
{
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/three_d_shop_db");
dataSource.setUsername("*******");
dataSource.setPassword("*******");
return dataSource;
}
}
Main page using css styles:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>3d_shop</title>
<link rel="stylesheet" type="text/css" href="/static/css/styles.css">
</head>
<body style="margin: 0">
<div class="page">
<header th:insert="~{other/fragments :: header}"></header>
<main style="margin-left: 10%; margin-right: 10%;">
<h2> Welcome! </h2>
<button>Text</button>
<div>
<p>
ipsum dolor sit amet, consectetuer adipiscing elit, sed diem
nonummy nibh euismod tincidunt ut lacreet dolore magna aliguam erat volutpat.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diem
nonummy nibh euismod tincidunt ut lacreet dolore magna aliguam erat volutpat.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diem
nonummy nibh euismod tincidunt ut lacreet dolore magna aliguam erat volutpat.
</p>
</div>
<div class="center_frame"></div>
</main>
<footer th:insert="~{other/fragments :: footer}" style="margin-top: auto;" ></footer>
</div>
</body>
</html>
Project hierarchy:
Upvotes: 1
Views: 615
Reputation: 125262
The /WEB-INF/classes/
folder is basically the root of the class path, so instead you should use the classpath:
prefix.
For the path section it is for matching incoming requests to see where to retrieve the resources from. I doubt that that has /WEB-INF/classes/
in the URL, so remove it from that part.
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/")
This is what you should end up with for a proper mapping.
Upvotes: 1