404 in css file after spring mapping

Asked

Viewed 267 times

0

Good night to you all!

I can’t 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();
}

And follow 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’t access css.

  • Where you put your css file?

  • The css file is in Resources inside the src/main folder.

2 answers

0

Hello, when creating links to Sources it is good practice to use the context of the application. You can use jstl to insert 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">

In order for the < c:url /> tag to work you need to import the jstl core library.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  • I’ve tried this process but I can’t, the only way I could was to create a Resources folder inside the webapp folder.

0


The problem is that you are mapping the file to a folder called resources inside the briefcase src/main/webapp(which is the place indicated for these acquisitions), but is putting the files in the folder src/main/resources (which is suitable for files in your application, such as configuration files).

Create a folder resources inside the briefcase src/main/webappand put your briefcase css inside.

In case you want, for some reason, to keep your files css inside the briefcase src/main/resources, create within that folder another folder called resources, put your briefcase css inside and change the mapping to:

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

Browser other questions tagged

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