Search code examples
springspring-mvcspring-securityspring-roospring-webflow

Spring MVC - mvc resource


I have a spring project where, on a linux/tomcat6 server we have an external folder for person photos inside "/var/projectname/personphotos". Tried making a symbolic link and the like and nothing seems to work, the application cannot see the external folder. I happened to stunble upon the <mvc:resources> tag and the <mvc:annotation-driven /> .How do I add it to the project ? I was thinking something like

<mvc:resources mapping="/personphotos/" location="/var/projectname/personphotos" />

and in the application i can have something like <img src="/personphotos/bla-bla.jpg"/>

All of the tutorials say to use the tags but they don't say where to place it. I assume the location must be understood, forgive my ignorance. I tried adding it to the applicationcontext.xml and that blew-up with errors. Other tutorials were saying to edit the servlet-context.xml, wherever that is ? see here

I am using spring 3.0.7 with STS and I tried changing to 3.1.0 in the pom file. The version change caused mvn tomcat:run to give some errors. I saw somewhere that the mvc:resource tage was available in 3.0.4 or highter, i guess I am safe for now. Please help to clear up these ambiguities.


Solution

  • It has to be in your servlet-context.xml file, probably this way:

    <mvc:resources mapping="/personphotos/**" location="/var/projectname/personphotos" />
    

    and you can access it using:

    <img src="${pageContext.request.contextPath}/personphotos/bla-bla.jpg"/>
    

    the starting part is to make sure that you application context part gets added in too.

    EDIT For the benefit of others, the Final thing which is working is

    <mvc:resources mapping="/personphotos/**" location="file:/var/projectname/personphotos" />