3. Image Interceptor

Used for generic thumbnail processing. If a request for '/pics/newyork.html' is received, the image interceptor will look for a directory at '/var/www/html/images/newyork'. If the directory exists generate any missing thumbnails and have the view rendered by the imageViewName property.

Spring Configuration

                    
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
	                       http://www.springframework.org/schema/context/spring-context.xsd">

    <!--Setup interceptor for annotation-based controllers -->
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <list>
                <ref bean="localeChangeInterceptor"/>
                <ref bean="imageInterceptor"/>
            </list>
        </property>
    </bean>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

    <context:annotation-config />

    <bean id="tilesConfigurer"
          class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles-defs/templates.xml</value>
            </list>
        </property>
    </bean>

    <bean id="dynamicTilesViewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springbyexample.web.servlet.view.tiles2.DynamicTilesView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <value>messages</value>
        </property>
    </bean>
    
    <!-- Declare the Interceptor -->
    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="locale"/>
    </bean>
    
    <!-- Declare the Resolver -->
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
 
    <bean name="imageInterceptor" class="org.springbyexample.web.servlet.image.ImageInterceptor">
        <property name="imageProcessor">
            <bean class="org.springbyexample.util.image.ImageProcessorImpl" />
        </property>
        <property name="rootImagePath" value="/var/www/html" />
        <property name="imageViewName" value="/pics/generic_display" />
    </bean>

    <bean class="org.springbyexample.web.servlet.mvc.GenericHtmlController" />
    
</beans>
                    
                

JSP Example

JSP referenced by the imageViewName property defined in the imageInterceptor bean.

                    
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<table>
<c:forEach var="item" items="${imageList}" varStatus="status">
    <c:if test="${(status.index % rowWidth) == 0}"> 
        <tr>
    </c:if>

    <td>
        <a href="${item.imagePath}"><img src="${item.thumbnailPath}"></a>
    </td>

    <c:if test="${(status.index % 3) == (rowWidth -1)}"> 
        </tr>
    </c:if>
</c:forEach>
</table>