404 in css file after spring mapping

0

Good night everyone!

I can not access the css of my application in spring, even after mapping resouce, follows codes:

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

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

E Here is the html of it:

<link href="resources/css/bootstrap/bootstrap.css" rel="stylesheet" media="screen">

What will be the problem, because even after maper I can not access the css.

    
asked by anonymous 30.01.2017 / 00:57

2 answers

0
The problem is that you are mapping the file to a folder named resources within the src/main/webapp folder (which is the location for those files), but you are putting the files in the src/main/resources folder (which is indicated for files of your application, such as configuration files).

Create a resources folder inside the src/main/webapp folder and place your css folder in there.

In case you want to keep your css files inside the src/main/resources folder, create a folder named resources inside that folder, put your css folder in there and change the mapping to:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/");
}
    
30.01.2017 / 18:00
0

Hello, when creating the links to the resources it is good practice to use the context of the application. You can use jstl to input the context dynamically, so you can write this link as follows:

   <link href="<c:url value='resources/css/bootstrap/bootstrap.css'/>" rel="stylesheet" media="screen">

For the < c: url / > run you need to import the jstl core library.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    
30.01.2017 / 15:00