Images not found in Spring - can not find resources folder - spring

I have the following folder structure. I added mvc annotation and resources path but when I try call the resources in home.jsp like <img src= "/resources/images/spitter_avatar.png" />, it can't find anything.
Folder structure:
Here is my code in my servlet-config.xml file for resources:
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/" />

<img src= "resources/images/spitter_avatar.png" />
try above remove "/" before resources

if you wanted to start with / , then use JSTL C tag
<img src= "<c:url value="/resources/images/spitter_avatar.png"></c:url>" />

The resources folder must be located inside the WebContent folder.
Place ${pageContext.request.contextPath} before your items url on the jsp page. Example:
${pageContext.request.contextPath}/resources/images/springlogo.svg

Related

How can I access specific one file from resource mapping in spring

I have the following servlet mapping present -
<mvc:resources mapping="/js/**" location="/resources/js/" />
Observation:
If any web request starts with the /js request path, then it will be mapped to the resources directory, and the /** symbol indicates the recursive look for any resource files underneath the base resource directory. Here I can access js/test.js, js/test2.js
Requirement :
I want to access only one specific file(js/test.js). How can I access specific one file(js/test.js) not all files which present into /resources/js location.
Can I use below servlet mapping :::
<mvc:resources mapping="/js/test.js" location="/resources/js/" />
When you add this line
<mvc:resources mapping="/js/**" location="/resources/js/" />
Spring will add a filter rule which says
Whenever any URL with /js/** is fired, spring look for it recursively in /resources/js/ folder.
When you add following configuration
<mvc:resources mapping="/js/test.js" location="/resources/js/" />
Only URL with /js/test.js will be looked in /resources/js/ folder.
Please be mindful that any other request eg /js/jquery or js/abc will not be filtered.

spring mvc - how to remove the word 'resources' when calling js,css,images

I have a resources folder in my WebContent folder that contains css, js, and images... Then i declare mvc resource mapping to locate all the resources file... but when i tried to call/acccess the file in my jsp, why is that i need to include the word resources?... do you know how can i remove it but still able to access my resources?
project structure
WebContent
|_resources
|_images
|_js
|_WEB-INF
|_jsp
|_header.jsp
header.jsp
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<script src="<c:url value="/resources/js/jquery.min.js" />"></script>
dispatcher.xml
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
above code works perfectly, but i want to exclude the word resources in path..
without writing one by one all the inside resources folder (like sample below)
<mvc:resources mapping="/images/**" location="/resources/images/" />
<mvc:resources mapping="/css/**" location="/resources/css/" />
what i need is something like this... but still declaring one line mvc resource mapping
<script src="<c:url value="/js/jquery.min.js" />"></script>
<link rel="stylesheet" href="<c:url value="/css/owl.carousel.min.css" />">
<img src="<c:url value="/images/pix/world.jpg" />" alt="user">
Do you use the server is tomcat? If you use the server is tomcat, you can do the following configuration
<Url-pattern> / </ url-pattern> means to intercept all requests.
If you need to deal with static resources js, css, etc., you can add in web.xml written before dispatcherServlet
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
Default is Tomcat's default servlet!

SpringMVC - cann't able to acces JS and CSS file in index.jsp

I have added
<mvc:resources mapping="/webapp/**" location="/webapp/" />
<mvc:default-servlet-handler />
in my dispatch-servlet.xml
my folder hierarchy is
enter image description here
and in index.jsp i declared
<spring:url value="/webapp/resources/js/bootstrap.min.js" var="bootstrapJS" />
<script src ="${bootstrapJS}" type="text/javascript"></script>
can any one tell the actuall issue is with code or folder hierarchy
Hi have you included the tag at the top of index.jsp it will look something like this. <%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>

Paths in an Intellij Spring-project

So I am currently working on a Spring-project in Intellij. In my JSP-view I have both JQuery, Bootstrap and some local css/js-files.
When I work on the pure HTML in WEBSTORM the paths are easy to interpreter as they just are relative. As far as I understand the paths in Spring are defined in a XML-file and everything goes from there.
My mvc-dispatcher-servlet.xml has this:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
And my files are organized like this:
WEB-INF
css
bootstrap.css
login.css
js
All js files.js
pages
login.jsp
menu.jsp
How do i find the path? Have google like a hero and tried a lot but the files are not found Are there other files that rules over the path-hierarchy?:(:(
Example:
<script src="relative-path/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="relative-path/css/login.css">
Assuming you are using maven:
src/main
java
resources
webapp
static
css
js
...
Then the config..
<mvc:resources mapping="/static/**" location="/static/"/>
then you can use /css/main.css, something like this to access your resources in jsp
But there are different options out there, I will suggest you to read the document

Resource : Single location to multiple mappings

When I use in my application:
<resources location="/resources/favicon.ico" mapping="/favicon.ico" />
to map out single resource.
How can I map single resource to be accessible through multiple mappings without need to duplicate the file locally ?
For example like this:
<resources location="/resources/favicon.ico" mapping="/favicon.ico" />
<resources location="/resources/favicon.ico" mapping="/favicon.png" />
You can include wildcards. mapping="/favicon.*"

Resources