spring mvc touch file and serve it as resource - spring

How can I create a file and serve it as a resource, my /src/main/webapp/WEB-INF/spring/web/dispatcherServlet-context.xml file snippet:
<mvc:resources mapping="/resources/**" location="/resources/" />
Any nice approaches?

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.

Static resource mapping in Spring3

My project is implemented in Spring3 and configuration is almost xml.
Configuration of static resource mapping is like below:
<mvc:resources mapping="/a/**" location="file:/data/a/"/>
There is no problem when accessing /a/xxx.jpg but 404 Error is occurred when accessing subdirectory like /a/b/yyy.jpg. There is no problem about access authority of filesystem. How can I solve this problem? Additional configuration is needed?
(Add) Project directory is fine, but filesystem directory does not work
Use something like below :
<mvc:resources location="/" mapping="/resources/**"/>
You can specify classpath also to location any particular folder like below.
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**"/>

Images not found in Spring - can not find resources folder

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

JBOSS_HOME spring resource mapping

Hi guys I can't properly map my JBOSS_HOME location for example I have something like this:
<mvc:resources order="-10" location="file:/C:/Serwery/jboss-as-7.1.1.Final/" mapping="/test/**" />
But this is absolute path I want to make something like this:
<mvc:resources order="-10" location="file:/$JBOSS_HOME/" mapping="/test/**" />
Any advice ?
Just add this
<context:property-placeholder />
to your xml configuration where you have the <mvc:resources /> and use it like this (notice how the env variable is handled - ${JBOSS_HOME}):
<mvc:resources order="-10" location="file:/${JBOSS_HOME}/" mapping="/test/**" />

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