问题
I am trying to load the css file into jsp, it is not loading and showing the message Failed to load resource: the server responded with a status of 404 (Not Found).the following is my code
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Bootstrap Form With Spring Mvc Example</title>
<link href="<c:url value='/resources/css/bootstrap.css'/>" rel="stylesheet" media="screen">
</head>
in configuration class I have added the below code:
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
I have tried all the solutions, I did not find what's the wrong,can any one help me?
回答1:
Try it this way:
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**")
.addResourceLocations("/css/");
}
This assumes your directory structure is:
src
main
java
resources
css
webapp
The point is that you don't specify the resources level in your resource handler.
回答2:
Is your css file in WebContent folder..Please check for it.And if your are using maven it must be in resources folder.If the folder structure is correct.
Please treat the jsp pages like html .. paste your css file near your jsp folder.And try to access it using href directly instead of using c tag.
<link href="../css/bootstrap.css" rel="stylesheet" media="screen">
This approach is dirty one.But good for jsp pages integrated thymeLeaf kindof tools.
回答3:
Edit your configuration class to inherit from WebMvcConfigurerAdapter as shown below. It solved my own problem after trying a lot of other options. Hope it would work for you.
public class Config extends WebMvcConfigurerAdapter{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
来源:https://stackoverflow.com/questions/31276757/failed-to-load-resource-the-server-responded-with-a-status-of-404-not-found-i