Setup
servlet-context.xml
<resources mapping="/resources/**" location="/resources/" />
.jsp
<script type="text/javascript" src="/resources/js/test.js"></script>
file location
webapp/resources/js/test.js
Problem
When I access localhost:8080/myApp/resources/js/test.js it is found but what is actually rendered in the page is localhost:8080/resources/js/test.js so /myApp is missing.
Is this really the case for localhost or is there something wrong with my setup?
This is a fresh Spring MVC Project made using STS (template) so I doubt it's the setup because it's already there from the start.
You have to add the context path when declaring the script
<script type="text/javascript" src="${pageContext.request.ContextPath}/resources/js/test.js"></script>
Assuming the script is declared in your jsp page. The context path points to the root of your app.
Related
I have a Spring boot web application, it works fine when running as executable jar, but when I build as war and deploy to Tomcat, the css, js files are not loading and looking into further, I found out all links are pointing to root context path "/", I tried changing base href = "./" this fixed the problem of loading css and js, but I need to do that in lot of pages.
Also the links in <a> still points to root context path and to make this work I need prefix'./' . Below are my code snips,
CSS and JS link
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/signin.css" rel="stylesheet">
<script src="js/popper.min.js"></script>
<script src="js/bootstrap.min.js"></script>
Tag
<a class="topHeader" href="/activateAccount"></a>
Is there a shortcut to fix this problem or should I change <base href> and <a href> in all the pages? Any help is highly appreciated.
There is no short cut or quick fix for this, I removed "/" in links in all anchor tags and that worked for me.
Example
<a href="/resetPassword" style="float: right;">
changed to
<a href="resetPassword" style="float: right;">
I'm having issues getting my css sheets to load in the browser for my spring webapp - I keep getting the following error:
The stylesheet http://localhost:8080/MyBudget/resources/css/hello.css
was not loaded because its MIME type, “text/html”, is not “text/css”.
File Strucutre
In my web.xml file, I have specified my resources as following;
<mvc:resources location="/resources/core/" mapping="/resources/**"/>
In my view (.JSP), I'm referencing the css file as following:
<link rel="stylesheet" href="<c:url value="/resources/css/hello.css"/>" type="text/css" />
I have tried just about everything to get this to work, and nothing - any suggestions?
Hi Thats coused by mvc configuration I think,
are you using load and startup configuraiton ?
I had same problem I solved by
spring.mvc.servlet.load-on-startup=1
to add my application.properties file
I read from here
https://www.javatpoint.com/load-on-startup
Learning JSP and have an issue with the css/js content not loading. I have a jsp page, where I have bootstrap css and js referenced using standard html link and script tags:
<link rel="stylesheet" type="text/css" link="/bootstrap/css/bootstrap.css" />
and
<script src="/WebIntro/bootstrap/js/bootstrap.js"></script>
neither of them work and Chrome is giving me the following on the console:
Resource interpreted as Stylesheet but transferred with MIME type text/plain:
If I use an include directive, it works for the css but pulls all the content into the file:
<%#include file="/bootstrap/css/bootstrap.css"%>
The jsp has the following #page and meta tags:
<%#page contentType="text/html" %>
and
<meta http-equiv = "Content-Language" content = "en"/>
<meta http-equiv = "Content-Type" content="text/html; charset=utf-8">
I tried googling and found the mime-mapping element for the web.xml but the following seems to have no affect:
<mime-mapping>
<extension>css</extension>
<mime-type>text/css</mime-type>
</mime-mapping>
<mime-mapping>
<extension>js</extension>
<mime-type>application/javascript</mime-type>
</mime-mapping>
If there is some standard Tomcat config that needs to happen I am unaware as I am new to Tomcat; using Tomcat 7, and eclipse and this is in a maven project.
The solution for me was very strange. The changes to the web.xml file that was present since I built the project in eclipse/maven, weren't reflected in the web.xml within tomcat webapp application directories. Everything else in the package updates fine (as far as I can tell), except for the web.xml?
So I found the web.xml file within eclipse inside target >> project-SNAPSHOT >> WEB-INF >> web.xml (which is supposed to be derived). Forced changes there and it worked, changes were picked up and my original issue gone.
I am guessing I had inadvertently broken some type of dependency link or something along the way somehow, but once I added the servlet, mappings, etc back in, everything worked fine.
Weird.
Removing this line in jsp resolved the error for us.
We were facing the issue when using Tomcat 8.5.59
I'm trying to implement the versioning when serving static resources with Spring MVC. I need to force the clients reload the resources when a new version of the application is deployed.
I've followed the spring official guide here and I cannot let it work with the mvc:resources, but it works in the JSP pages.
Here my configuration in springMVC-servlet.xml:
<mvc:resources mapping="/style-#{applicationProps['version']}/**" location="/style" />
...
<util:properties id="applicationProps" location="/WEB-INF/conf/application.properties"/>
and this is the part of the jsp that is working:
<spring:eval expression="#applicationProps['version']" var="applicationVersion"/>
<link rel="stylesheet" type="text/css" href="/style-${applicationVersion}/style-common.css">
and finally this is the page source in the browser:
<link rel="stylesheet" type="text/css" href="/style-2.1-SNAPSHOT/style-common.css">
The problem is that this file is not mapped. When I click on it it says: resource not found...
Any idea how to proceed, debug, understand the issue?
Many thanks!
Replace
<mvc:resources mapping="/style-#{applicationProps['version']}/**" location="/style" />
By
<mvc:resources mapping="/style-#{applicationProps['version']}/**" location="/style-#{applicationProps['version']}/" />
Resource location attribute should be complete folder name.
I am using Eclipse Juno Service Release 1, instaled the latest GWT Designer plugin and run update. When I am in GWT Design and I select the CSS button in the StyleName row of a widget's properties I get the message "There are no CSS files referenced from module or HTML".
My gwt.xml is:
<module>
<inherits name="com.google.gwt.user.User"/>
<inherits name="com.google.gwt.user.theme.standard.Standard"/>
<entry-point class="org.AwardTracker.client.AwardTracker"/>
<stylesheet src="org.AwardTracker.AwardTracker.css"/>
<!-- servelet context - path is arbitrary, but must match up with the rpc init inside java class -->
<!-- Tomcat will listen for this from the server and waits for rpc request in this context -->
<servlet class="org.AwardTracker.server.MySQLConnection" path="/MySQLConnection" />
<inherits name="com.google.gwt.user.theme.standard.Standard"/>
<inherits name="com.google.gwt.user.theme.chrome.Chrome"/>
<inherits name="com.google.gwt.user.theme.dark.Dark"/>
</module>
My CSS is in org.AwardTracker (directly above the gwt.xml file).
All the hints that I have googled have been to update GWT Designer. These hints are all several years old and I have the latest version.
Any idea what I am doing wrong?
You have to first chk your css file in war folder bcoz css file and html file exist in war folder than check link in html file
<link type="text/css" rel="stylesheet" href="abc.css">
You need to have your project packages and references correct. You can follow the solution offered here regarding project/file organization - Editing CSS in Gwt WindowBuilder
It seems the file is not found due to the path.You are already in The xml file so try
<stylesheet src="AwardTracker.css"/>
Hope this will helpfull .