Css only applies on one page, using Spring MVC

Asked

Viewed 98 times

0

Hello, I’m doing a project using Spring MVC I’m applying a css to the main page and it’s all right.

<link href="${contextPath}resources/css/reset.css" rel="stylesheet" type="text/css" media="all"/>
 <link href="${contextPath}resources/css/style.css" rel="stylesheet" type="text/css" media="all"/>
 <link href="${contextPath}resources/css/index.css" rel="stylesheet" type="text/css" media="all"/>

but when I apply the css to another page it does not work and I am using the same css described above. I wonder if someone could help me.

Note. I’ve done all the configuration in the Springmvc configuration file.

1 answer

1


Instead of using the ${contextPath}, the best way to do with Spring is to use a framework resource to map the resources of the application.

If you are using configuration via XML, it would be like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 ...

 <!-- Mapeia os recursos abaixo da pasta resources para estarem disponíveis no path /resources/ -->
 <mvc:resources mapping="/resources/**" location="/resources/" />

 ...

</beans>

Or using WebMvcConfigurer:

@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {

    ...

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");

    }

    ...
}

And on your page you would use only:

<link href="/resources/css/reset.css" rel="stylesheet" type="text/css" media="all"/>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.