com.smartgwt.client.docs

## Interface SpringIntegration

-     public interface SpringIntegration

  ### Integration with Spring

  Overview

  The Spring framework has many different parts, from integration with Object Relational Mapping (ORM) and transaction management systems, to a Model View Controller (MVC) architecture.

  If you are building a new application from scratch and/or you are trying to modernize the presentation layer of an existing application, most of Spring MVC is inapplicable in the `Smart GWT architecture`. Specifically, Smart GWT renders all HTML on the client, and the server is responsible only for retrieving data and enforcing business rules. This means that Spring's ModelAndView and all functionality related to retrieving and rendering Views is unnecessary in Smart GWT. Smart GWT only needs the Model, and provides methods to deliver that Model to Smart GWT components (the server side method DSResponse.setData()).

  However, Spring's DispatchServlet, Handler chain, and Controller architecture is applicable to Smart GWT. See "Using Spring Controllers" below.

  Existing Spring Application

  As discussed under the general `server integration` topic, integrating Smart GWT into your application involves finding a way to provide data that fulfills the `DataSource requests` sent by Smart GWT components.

  There are 2 approaches for integrating Smart GWT into an existing Spring application:

  - call Spring beans via Smart GWT DMI [Recommended]: use Smart GWT Direct Method Invocation (DMI) to map `DataSource requests` to beans managed by Spring, via `lookupStyle`:"spring". Return data to the browser by either simply returning it from your method, or via creating a DSResponse and calling DSResponse.setData() (server-side method).

    This is the easiest method and produces the best result. A Collection of Java Beans, such as EJB or Hibernate-managed beans, can be directly returned to Smart GWT as the result of a DMI method, without the need to create an intervening Data Transfer Object to express which fields should be delivered to the browser - instead, only the fields declared on the DataSource are returned to the browser (see `dropExtraFields`. In this integration scenario, the majority of the features of the Smart GWT Server framework still apply - see this `overview`.

  - configure Spring to return XML or JSON responses: create variants on existing Spring workflows that use a different type of View in order to output XML or JSON data instead of complete HTML pages. The Smart GWT `RestDataSource` provides a standard "REST" XML or JSON-based protocol you can implement, or you can adapt generic `DataSources` to existing formats.

    In some Spring applications, all existing Spring workflows can be made callable by Smart GWT with a generic View class capable of serializing the Model to XML or JSON, combined with a Controller that always uses this View. Consider the following Java anonymous class, which uses the Smart GWT JSTranslater class to dump the entire Spring Model as a JSON response.

           new View() {
                 public void render(Map model, HttpServletRequest request,
                                    HttpServletResponse response) throws IOException {
                         final ServletOutputStream outputStream = response.getOutputStream();
                         response.setContentType("application/x-javascript");
                         outputStream.println(JSTranslater.get().toJS(model));
                         outputStream.close();
                 }
                 public String getContentType() {
                         return "application/x-javascript";
                 }
           }

    If you use this approach, you do not need to install the Smart GWT server, and can `deploy` Smart GWT as simple web content (JS/media/HTML files). If you are already familiar with how to generate XML from objects that typically appear in your Spring Models, this may be the easiest path.

  Using Spring Controllers with Smart GWT DMI

  You can create a Controller that invokes standard Smart GWT server request processing, including DMI, like so:

        public class Smart GWTRPCController extends AbstractController
        {
            public ModelAndView handleRequest(HttpServletRequest request,
                                              HttpServletResponse response)
                throws Exception
            {
                // invoke Smart GWT server standard request processing
                com.isomorphic.rpc.RPCManager.processRequest(request, response);
                return null; // avoid default rendering
            }
        }

  This lets you use Spring's DispatchServlet, Handler chain and Controller architecture as a pre- and post-processing model wrapped around Smart GWT DMI.