Spring by Example Web Module

David Winterfeldt

2008


There is a Spring GWT Controller for standard GWT usage and also Spring Bayeux integration for using Comet on Jetty.

There is an ImageInterceptor that intercepts a request and looks in a directory that matches the requests relative path. Currently it only matches one image extension type which defaults to '.jpg'. It generates a thumbnail if one doesn't exist and also makes a list of available thumbnails and images.

1. Spring GWT Controller

Spring Configuration

Even though the service controller is annotation-based, since GWT calls RPC methods using reflection, the mapping has to be manually set using the SimpleUrlHandlerMapping.

                    
<?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">

    <context:component-scan base-package="org.springbyexample.web.gwt.server" />

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="order" value="0" />
        <property name="mappings">
            <value>
                /person/service.do=serviceController
            </value>
        </property>
    </bean>
    
</beans>
                    
                

Code Example

                    
@Controller
public class ServiceController extends GwtController implements Service {

    final Logger logger = LoggerFactory.getLogger(ServiceController.class);

    private static final long serialVersionUID = -2103209407529882816L;

    @Autowired
    private PersonDao personDao = null;
    
    /**
     * Finds person within a range.
     */
    public Person[] findPersons(int startIndex, int maxResults) {
        Person[] results = null;

        List<Person> lResults = new ArrayList<Person>();

        Collection<org.springbyexample.orm.hibernate3.annotation.bean.Person> lPersons = personDao.findPersons(startIndex, maxResults);
        
        for (org.springbyexample.orm.hibernate3.annotation.bean.Person person : lPersons) {
            Person result = new Person();
            result.setId(person.getId());
            result.setFirstName(person.getFirstName());
            result.setLastName(person.getLastName());
            
            lResults.add(result);
        }

        return lResults.toArray(new Person[]{});
    }

}