I am new to spring and am trying it.
I have a jsp file which includes css and images as follows
<link href="css/style.css" rel="stylesheet" type="text/css" />
Now i have a url path as /localhost/myapp/test/tst which is mapped to a controller which then forwards it to view jsp
view.jsp is present it
/web-inf/jsp/view.jsp
But when hit the path /localhost/myapp/test/tst the css and images path gets resolved to
/localhost/myapp/test/tst/css/style.css
Actaully i want it to come as /localhost/myapp/css/style.css
css and html are present in root of webapp
How i have following in my spring config
<mvc:annotation-driven />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/css/**" location="/css/" />
Your CSS href is currently relative to your page, you need to make it relative to the server root by adding a leading slash (plus context, etc). Its also a good idea to add the contextPath so that if/when you change the webapp name or deploy it as a root webapp, the resources still work.
<link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet" type="text/css" />
Related
I'm very new to Spring mvc,for this question i've found many answers here but nothing is working for me and please help me to find out what mistake i'm doing here.
I have created a css file under WebContent>WEB-INF>resources>css>style.css and mapping is done under spring-servelet.xml and the code is
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<context:component-scan base-package="com.journaldev.spring" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Handles HTTP GET requests by efficiently serving up static resources
in the corresponding directory -->
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<mvc:resources mapping="resources/**" location="resources/" cache-period="31556926"/>
</beans:beans>
and i've called the css file from jsp and the jsp file is
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>welcome</title>
<!-- Custom Theme files -->
<link href="<spring:url value='resources/css/style.css' />" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<h1>Hello world!</h1>
<P>The time on the server is ${serverTime}.</p>
</body>
</html>
Now network is aborted error is displaying at console .As i told i'm newbie to this framework so please help me out other than giving a link to an answer because i've tried almost all.Thank you
Put your CSS folder inside webcontent and add below code servlet xml file
<mvc:resources mapping="/css/**" location="/css/" />
Now you will be able to take the CSS from JSP inside views folder using below code -
<link href="/css/yourCSS.css" rel="stylesheet" type="text/css" media="all" />
This should work otherwise let usknow!
So, you will need to do what vipin cp is suggesting. You need this:
<link href="<spring:url value='/css/style.css' />" rel="stylesheet" type="text/css" media="all" />
This is because the you are using the mapping mapping="/css/**" , so your value needs to begin with the same thing as your mapping "/css/...", without the beginning slash, it doesn't match your mapping so spring doesn't know where to map it too, you could also change your mapping to mapping="css/**". Which ever you choose, they just need to match each other.
Once they match Spring can then resolve your urls to what they need to be.
Also, just a suggestion for readability. Specify all your spring:url elements at the top with a var argument and then you can reference that with standard SpEl.
And example is:
<spring:url value='/css/style.css' var='styleCss'/>
So, that tells Spring that the styleCss variable references the resolved url from /css/style.css. You can then reference that variable like:
<link href="${styleCss}" rel="stylesheet" type="text/css" media="all" />
Obviously, up to you, but I have always found it more readable.
For what it is worth, this can also be done with your JS imports if you have any.
I use eclipse, springmvc and maven. I created a folder under
Web-Content --> WEB-INF --> resources
I added the following into my servlet:
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
<mvc:annotation-driven />
Then I try to include a picture into my index.jsp file.(Web-Content->index.jsp)
<img src = "/resources/images/Logo.png" />
But it's not loading my image. I tryed many tutorials but it doesn't work. I guess I'm something basic wrong. Can you tell me whats wrong?
EDIT:
my serlvet got this
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
In my jsp file I did this:
<img src="<c:url value="/resources/logo.png" />"/>
I got resource folders with the logo.png file in the following places:
main->resources
main->webapp->resources
main->webapp->WEB-INF->resources
It still doesn't work
I also tried :
<mvc:resources mapping="/resources/**"
location="/, classpath:/WEB-INF/public-resources/"
cache-period="10000" />
and include the picture with
/resources/images/logo.png
and the picture located in
src/main/webapp/images/logo.png
doesnt't work for me
EDIT:
I guess it this:
<mvc:resources mapping="/resources/**" location="/resources/" />
got no effect to the program. I can always acces the url http://localhost:8080/test/resources/logo.png
doenst matter if I got this mvc line in my serlvet or not. If I change
<mvc:resources mapping="/resources/**" location="/resources/" />
to
<mvc:resources mapping="/resources1/**" location="/resources/" />
I can't acces to the file with http://localhost:8080/test/resources1/logo.png but with http://localhost:8080/test/resources/logo.png
Greets Sam
As an aside, while it is probably not best to make an Eclipse Dynamic Web Project and convert it to a Maven project through Eclipse, it will work, because Eclipse will put an entry into your POM that points to the WebContent directory. Have a look you will probably find it there. There is nothing wrong with making a Dynamic Web Project per se, but I typically change the initial directory configuration to match maven's expectations in the Wizard and convert to a maven project right away. Much easy to work on the POM than copy jars to the lib directory.
Otherwise, the resources directory you referring to should be in the root of your war file. The best way to get it there is to put resources at the root of your WebContent directory, not under WEB-INF. Try moving it there, open the .war file and inspect the contents to be sure it is in the root, and you should be good to go.
I have a Spring Boot Application. I am trying to load to my HTML file a CSS file, but probably I am doing something wrong. In Spring MVC Project I've done something like this:
In HTML file I added the following line:
<link href="<c:url value="/resources/css/style.css" />" rel="stylesheet">
And in mvc-dispatcher-servlet.xml I added following code:
<mvc:resources mapping="/resources/**" location="/resources/"
cache-period="31556926"/>
<mvc:annotation-driven />
And everything worked perfect.
Now I'm using thymeleaf templating language and load my CSS file like that:
<link rel="stylesheet" type="text/css" th:href="#{/assets/css/style.css}" />
My project files hierarchy is following:
In current project I don't have WEB-INF directory so I don't have such a files like mvc-dispatcher-servlet.xml or web.xml so I don't know if I should or where I should paste that code with resources mapping. Im completely new to Spring so I apologise for my lack of knowledge.
Any tips and solutions will be much appreciated.
This is my dispacter-servlet.xml file. When i deploy the project on resin pro 4.0.36 it loads my index page and the content BUT FAILS to load css files and images stored under the staticresources folder
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.dogears" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<import resource="classpath:beans.xml"/>
<mvc:resources mapping="/resources/**" location="/staticresources/" />
<mvc:annotation-driven />
Please can anyone tell me how to mapp my static resources folder, so that whenever the request is of the patter /resources/* it redirects the request to the static resources folder. the staticresources folder is in MyspringProject/src/main/webapps directory.
I believe you need to map your staticresources directory as a root directory. In Eclipse that would be a "Source Folder" and in IntelliJ a "Resources Root".
Refer to the following help docs depending on your IDE:
Eclipse: http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-wizard-source-folder.htm
IntelliJ: https://www.jetbrains.com/webstorm/help/configuring-folders-within-a-content-root.html
Suppose your project directory structure is like
- src
- main
- webapp
- resources
- css
- abc.css
- images
- xyz.jpg
& your .html or .jsp pages are located in the directory as below
- webapp
- index.jsp
- pages
- welcome.jsp
then you can add link to your resources in index.jsp page with the URL BaseURL/index.jsp
<link href="resources/css/abc.css" rel="stylesheet" type="text/css" />
<body>
<img src="resources/images/xyz.jpg" alt="" style="width:100px;height:100px" />
</body>
& to welcome.jsp with URL BaseURL/pages/welcome.jsp as
<link href="../resources/css/abc.css" rel="stylesheet" type="text/css" />
<body>
<img src="../resources/images/xyz.jpg" alt="" style="width:100px;height:100px" />
</body>
Hope this helps you.
I finally found my solution. I had deployed my spring webapp in the webapps folder of the resin directory.
I realised that <mvc:resource /> mapping tag does not work when you have deployed your spring app on resin server instead of tomcat.
Hence i solved this by first creating a war file of my project and then extracted the war file on the desktop and later placed all the contents from the war file in the webapps/root (and not webapps folder) folder of resin directory. and then from my index page i used JSTL TAG to include the external stylesheet.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<link rel="stylesheet" type="text/css" href="<c:url value="/staticresources/externalcss.css"/>">
</head>
<body>
<h2 class="text_style">Hello World!</h2>
</body>
</html>
IT WORKS !!!
I am utilising the Bootstrap front end framework, although I am having some trouble getting the icons to load. I have implemented Spring security, I am not sure whether this may be affecting them not to load. I am calling my .css and .js files like this:
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/style/bootstrap.css" />
<script src="${pageContext.request.contextPath}/static/js/jquery.js"></script>
This is the configuration that I have in my Dispatcher servlet config:
<mvc:resources location="/resources/" mapping="/static/**" />
This is really irritating, can someone please help. Thanks
In your spring security config, have you excluded your resources directory containing your css, js, images from the secured zone ?
<http use-expressions="true" security="none" pattern="/resources/**">
</http>
If the problem is only related to the twitter bootstrap icons, have you included the files glyphicons-halflings.png glyphicons-halflingswhite.png in a resources/img folder ?