aspose file tools*
The moose likes Spring and the fly likes trouble loading css Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "trouble loading css" Watch "trouble loading css" New topic
Author

trouble loading css

Michael Keisu
Ranch Hand

Joined: Oct 08, 2009
Posts: 30
In my Spring MVC application, I have trouble loading .css resources.

In my Google Chrome console when I load the page in question:
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:8080/blog/css/main.css".

which is odd because this is what my link tag looks like:

and my folder structure looks like this
webapp->css
webapp->WEB-INF->views
etc, etc.

You'd think it had nothing to do with Spring(and it might not), but I have almost identical code in normal servlet projects which run smoothly.

Any ideas?
Thanks,
Mike
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Yep, it isn't a Spring issue. It is a Web context location issue.

I bring in my css files like

<link href="${pageContext.request.contextPath}/styles/themes/default.css" rel="stylesheet">

This is putting it at the root of the war file, then going into the /styles…. directory to find the css.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Pondurai Singh
Ranch Hand

Joined: Jun 27, 2011
Posts: 35
Mark Spritzler wrote:Yep, it isn't a Spring issue. It is a Web context location issue.

I bring in my css files like

<link href="${pageContext.request.contextPath}/styles/themes/default.css" rel="stylesheet">

This is putting it at the root of the war file, then going into the /styles…. directory to find the css.

Mark


Correct,


Software Developer, Oracle Java Certification Training Labs at EPractize Labs.
Michael Keisu
Ranch Hand

Joined: Oct 08, 2009
Posts: 30
Mark Spritzler wrote:Yep, it isn't a Spring issue. It is a Web context location issue.

I bring in my css files like

<link href="${pageContext.request.contextPath}/styles/themes/default.css" rel="stylesheet">

This is putting it at the root of the war file, then going into the /styles…. directory to find the css.

Mark


It does what you say. Only, it doesn't seem to solve the issue I have only used Spring for about a week, but I didn't try any css until now. (in any Spring projects I mean, I have known css some time)

still:
"Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:8080/blog/css/main.css"."
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

What did you change it too?

I would assume

<link href="${pageContext.request.contextPath}/css/main.css" rel="StyleSheet" />

Mark
Michael Keisu
Ranch Hand

Joined: Oct 08, 2009
Posts: 30
Mark Spritzler wrote:What did you change it too?

I would assume

<link href="${pageContext.request.contextPath}/css/main.css" rel="StyleSheet" />

Mark


Yes, also tried with type of text/css at the end
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Why in your first post do you have


<link href="/blog/css/main.css" rel="StyleSheet" type="text/css" />

with

"/blog"

That part shouldn't be in there. Since /css is at the root of your war file.

Anyway, my example is also assuming HTML5, so if you are using HTML4, then you might have to have the type="text/css"

Mark
Michael Keisu
Ranch Hand

Joined: Oct 08, 2009
Posts: 30
I put this in my spring-servlet.xml:
<mvc:resources mapping="/css/**" location="/css/*" />

and this in my jsp:
<link href="<c:url value="/css/main.css"/>" rel="stylesheet" type="text/css" />

that got it working, although I suspect it was just mvc:resources mapping that did it.

Anyway, thanks everyone.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Glad you got it resolved. Funny I have this in my xml file, put there by Spring ROO, but I don't use it because I don't have a /resources/** directory

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**"/>

Maybe the "/" in location is what gets it to work.

Mark
Michael Keisu
Ranch Hand

Joined: Oct 08, 2009
Posts: 30
Mark Spritzler wrote:Glad you got it resolved. Funny I have this in my xml file, put there by Spring ROO, but I don't use it because I don't have a /resources/** directory

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**"/>

Maybe the "/" in location is what gets it to work.

Mark


Yes, the "/" should be enough, although I used <mvc:resources mapping="/**" location="/*" /> to get all my resources(css, js, other stuff) to work(not sure if it makes any difference). By doing that I wasn't required to use
any further trickery, just importing css, js as I normally would in a regular HTML page.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Michael Keisu wrote:
Mark Spritzler wrote:Glad you got it resolved. Funny I have this in my xml file, put there by Spring ROO, but I don't use it because I don't have a /resources/** directory

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**"/>

Maybe the "/" in location is what gets it to work.

Mark


Yes, the "/" should be enough, although I used <mvc:resources mapping="/**" location="/*" /> to get all my resources(css, js, other stuff) to work(not sure if it makes any difference). By doing that I wasn't required to use
any further trickery, just importing css, js as I normally would in a regular HTML page.


Yeah, I like that better than having to use the {path…….} in my html

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trouble loading css