1

This is where i put my css file:

enter image description here

This is how i register this resource:

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

This is how i references this css from the JSP:

<link type="text/css" href="/css/bootstrap.css" rel="stylesheet"/>

I can see the JSP page with content but without styling.

I think the error is while referencing but i cant find it. I have tried also these, but does not work:

<link type="text/css" href="../css/bootstrap.css" rel="stylesheet"/>

<link type="text/css" href="/resources/css/bootstrap.css" rel="stylesheet"/>

<link type="text/css" href="<%=request.getContextPath() %>/css/bootstrap.css" rel="stylesheet"/>

<link type="text/css" href="<%=request.getContextPath() %>/resources/css/bootstrap.css" rel="stylesheet"/>

Any idea?

EDIT: Now i have moved the css folder to WebContent. It seems like this:

enter image description here

It still does not work. What should i register for the ResourceHandler?

registry.addResourceHandler("/WEB-INF/**").addResourceLocations("/WEB-INF/"); ?

How should i reference the css from the JSP?

2 Answers 2

1

The resource handler doesn't look for resources on the class path, by default. It looks for them in the webapp.

Create a folder called resources and put it in /src/main/webapp. Then put your css, js, etc. folders in there.

Sign up to request clarification or add additional context in comments.

5 Comments

The resource handler doesn't look for resources on the class path, by default I dont understand what the "default" is. registry.addResourceHandler is just for that reason there, so that any file on the build path can be registered..
@Cimbom Where are your jsps? Those should typically be in the WEB-INF folder which is inside webcontent . With maven, you use src/main/webapp instead of webcontent.
@Cimbom If you want to specify files in the build path, the resource location needs to be prefixed with classpath:. I don't think that is a good idea.
I am using maven and i have a webContent folder. Please look at the question. I updated it. And please define a concrete solution.
@Cimbom Leave your original resource registration as is. Create a src/main/webapp folder. In eclipse, add it to the build path. It is now a source folder. Add a folder called resources to it. Move your css folder from src/main/resources to src/main/webapp/resources.
1

The files under src cannot be accessed on default condition , you should put these assets into WebContent directory.


Resources usually means config proppertis like properties and xml, they will be used by the java code, assets usually mean that can be accessed by the browser directly,Notice that WEB-INF is protected,you need to put your css directory under WebContent, except for WEB-INF

for exampel,you can put your css directory under /WebContent/assets/

And add these code at the head of your jsp pages

<%
String basePath = request.getScheme() + "://"
        + request.getServerName() + ":" + request.getServerPort()
        + request.getContextPath();
%>

<link type="text/css" href="<%=basePath%>/assets/css/bootstrap.css" rel="stylesheet"/>

It's not good to use relative path!!

7 Comments

I dont understand what the "default condition" is. registry.addResourceHandler is just for that reason there, so that any file on the build path can be registered..
Your last suggestion does not work. href seems like this "localhost:8080/projectName/assets/css/bootstrap.css" and the file not found. Also this should be made in another way, where you do not have to define a basePath. Since we are using Spring here, we should easily be able to register the css files, and these files should be reached easily just like the jsp file itself. I do not think that every JSP page in the application must define or need this constant basePath.
Ok. When i add the assess folder as resource, it works. registry.addResourceHandler("/assets/**").addResourceLocations("/assets/**"). I am trying to get rid of this basePath now..
href="../assets/css/bootstrap.css". Without basePath. It works.
maybe WEB-INF is protected, even spring resource handler cannot access it. LOL
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.