This is my project folder structure.
I have resources folder under webapp.
Source
_ main
-webapp
- resources
adminlogin.jpeg
I have one jsp page adminlogin.jsp which contain 1 image.But When ever I try to access this page I get page but not image.
This is my SpringDispatcher-servlet.xml
<i><context:component-scan base-package="ShoppingOnline" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:annotation-config />
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</i>
This is my jsp page
<html>
<body>
<img src="/adminlogin.jpeg" alt="ADMIN LOGIN">
</body>
</html>
I have seen many example in your site very one has given one soultion.
but no has explain how its work in folder structure with example.I have made project in eclipse with the help of maven and In my folder structure the resources file is inside the web-inf folder.I haven't seen a single good example in this sites.
Please if u explain,explain with a good example how it is work if the resource is inside the web inf folder and when the resources is outside the web inf folder.
Please also let me know my mistake.
Thanks for helping.
Add your static files to webapps > resources
servlet.xml
<mvc:resources mapping="/resources/**" location="/resources/" />
then static files can be accessible from the page.
in Jsp:
<img src="/resources/images/yourimage.jpg" />
Related
I am struggling with spring mvc file structure.
I am able to display an image ressource in a jsp but not to link a css to it, even if they are at the same location !
Here is my file structure :
/src
/main
/resources
/webapps
/resources
main.css
logo.png
/views
somePage.jsp
/WEB-INF
web.xml
dispatcher-servlet.xml
....
Here is what I have in my dispatcher :
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
And in my somePage.jsp :
<img src="<c:url value="/resources/logo.png" />" alt="blabla" />
<link href="<c:url value="/resources/main.css" />" rel="stylesheet" />
...
The image load correctly but not the css...
Note : I have the following message in eclipse : : "invalid location of tag (link). " about the css link...
If you have any idea, thank you by advance !
Finally it seems to be a cache issue.
I just delete my chrome cache and my css appeared.
When I compiled the first time, my css was empty and I filled it later.
It seems that even after editing it and re-running the project the css state ,wasn't refreshed.
chrome probably kept my css state as empty and didn't detect the update .. weird.
You add some code. like this.
Your servelet-context.xml
<mvc:default-servlet-handler />
Your jsp
<c:set var="contextPath" value="<%=request.getContextPath()%>"></c:set>
<link href="${contextPath}/resources/main.css" rel="stylesheet" />
I have mapped my resources locust.js and locust.css files to the folder, resources, as shown in the image:
Directory Structure
The mvc-config.xml file has the following configuration settings:
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
I am getting 404 error when I try to load the locust.js and locust.css files. The jsp files are rendering properly.
Any clue where am I wrong here?
Spring was unable load the locust.js and locust.css files because my root ("/") folder was pointing to "webapp" folder and so spring was unable to resolve the path as the resources were outside the 'webapp' folder.
Thanks.
I know this question has been asked countless times.I also found many solutions but the image is not displaying.Getting error resource not found.I have done a fairly basic example.
Please help me.
i am new to spring
spring-servlet.xml
<context:annotation-config />
<context:component-scan base-package="com.app" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/pages/" p:suffix=".jsp">
</bean>
In display.jsp i have written the following code
<img src="<c:url value="/resources/img/a.jpg" />" />
I have created the resouces/img folder directly under WebContent.
The error get in browser console
Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:7070/SpringStaticDemo/resources/img/a.jpg
Thank u for the help in advance and sorry for asking such a basic question.
Don't know what was wrong.But I restarted Eclipse and now everything is working fine.It is showing the image. Don't know what happened.
I am trying to give access to robots.txt in Spring-MVC. To test the code, I put robots.txt in WebContent,Root and WEB-INF but I cannot access to any of them.
I've already applied answers of these questions 1,2,3 to no avail.
MyCode
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/robots.txt" location="/robots.txt" order="0" />
<mvc:annotation-driven />
This works for me:
Put robots.txt directly under webapp
In mvc-dispatcher-servlet.xml have:
<mvc:default-servlet-handler/>
<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/web-resources/" />
Use maven to build your war
I configured my confugration file ie. dispatcher-servlet.xml file for Themes using following beans
<bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource">
<property name="basenamePrefix" value="theme-" />
</bean>
<!-- Theme Change Interceptor and Resolver definition -->
<bean id="themeChangeInterceptor" class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
<property name="paramName" value="theme" />
</bean>
<bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver">
<property name="defaultThemeName" value="default" />
</bean>
I have added 3 properties
as theme-black.properties,theme-blue.properties,theme-default.properties under the source directory.
in each properties file I added key-value pair as following
style=style/blue.css
style=style/black.css
style=style/default.css
i put style folder under Web-Content .
problem is this ResourceBundleThemeSource loaded properties file successfully but could not able to load css file.
In JSP file I have added follwing code
<link rel="stylesheet" href="<spring:theme code='style'/>"
type="text/css" />
for changing
<span style="float: right;"> <a href="?theme=default">
default</a> | blue | black
</span>
Please help me out if any issue is there........ please please please > Thanks in Advance
I don't have experience with Spring's theme support, however I spot a little mistake in your URL.
First your URLs are relative. You should always have absolute URLs (starting with /).
Second, when using any URL, you should use <c:url> or <spring:url> so that correct context prefix is used:
<spring:url var="cssUrl">
<jsp:attribute name="value"><spring:theme code="style"/></jsp:attribute>
</spring:url>
It is as simple as specifying the location of your static content (in you application-config.xml), and the path from which to access them:
<mvc:resources mapping="/resources/**" location="/resources/"/>
And next you edit the themes properties file with :
style=resources/style/blue.css
And put blue.css file into: webapp/resources/themes
Now you can load css file using:
"resources/themes/blue.css" in href
Or using style in code of spring:theme tag.
This works for me.
I don't know whether you have put the bean themeChangeInterceptor ref under bean DefaultAnnotationHandlerMapping . If you have done that , then please remove that from there and put that inside the <mvc:interceptors>tag like below
<mvc:interceptors>
<ref bean="themeChangeInterceptor" />
</mvc:interceptors>