2

I want to outsource my images outside the spring project folder. After some researches I found the mvc:resources tag, which seemed to be the perfect solution for my requirement.

app-servlet.xml

xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<mvc:annotation-driven/>
<mvc:resources mapping="/pics/**" location="file:/c:/Tomcat_6/webapps/external_resources/" order="0" />

JSP Call:

<img src="<c:url value="/pics/test.png"/>" />

I have no idea why this is not working for me.

Few hours later I have read that removing the following lines will solve the problem, but nothing happened.

<bean id="viewMappings" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property value="true" name="alwaysUseFullPath"></property>
    <property name="defaultHandler">    
        <bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
    </property>
    <property name="order" value="1"/>
</bean>

Also changing

<servlet-mapping>
    <servlet-name>onlinecatalog</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

to

<servlet-mapping>
    <servlet-name>onlinecatalog</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

did not help.

Alex Celeste
  • 12,824
  • 10
  • 46
  • 89
Tunguska
  • 1,205
  • 3
  • 18
  • 37

1 Answers1

1

You can use java based configuration like this (in linux)

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String rootPath = System.getProperty("user.home"); 
String imagePath = "file:"+rootPath + File.separator + "tmpFiles/"; //absolute path of the directory
System.out.println(imagePath);
registry.addResourceHandler("/resources/**").addResourceLocations("resources/");
registry.addResourceHandler("/tmpFiles/**").addResourceLocations(imagePath);

}

now you can access external files from the tmpFiles folder. in windows probably you may need your imagePath as

imagePath="file:c:/Tomcat_6/webapps/external_resources/";
Jebil
  • 1,144
  • 13
  • 25