net.sf.click.extras.spring
Class SpringClickServlet

java.lang.Object
  extended byjavax.servlet.GenericServlet
      extended byjavax.servlet.http.HttpServlet
          extended bynet.sf.click.ClickServlet
              extended bynet.sf.click.extras.spring.SpringClickServlet
All Implemented Interfaces:
Serializable, Servlet, ServletConfig

public class SpringClickServlet
extends ClickServlet

Provides an Spring framework integration SpringClickServlet.

This Spring integration servlet provides a number of integration options using Spring with Click pages. These options detailed below.

Spring instantiated Pages with Spring XML configuration

With this option Page classes are configured using Spring XML configuration. When the SpringClickServlet receives a page request converts the auto-mapped page class to the equivalent Spring bean name and gets a new instance from the Spring ApplicationContext.
 customer-list.htm  ->  com.mycorp.page.CustomerListPage  -> customerListPage
 HTML Request           Click Page Class                     Spring Bean Name 
If the page bean is not found in the ApplicationContxt then the full Page class name is used.
 customer-list.htm  ->  com.mycorp.page.CustomerListPage  -> com.mycorp.page.CustomerListPage
 HTML Request           Click Page Class                     Spring Bean Name 
This integration option requires you to configure all your Spring Page beans in your Spring XML configuration. While this may be quite laborious, it does support Spring 1.x and Java 1.4. An example page bean configuration is provided below:
 <?xml version="1.0" encoding="UTF-8"?>
 <beans>

    <bean id="customerListPage" class="com.mycorp.page.CustomerListPage" scope="prototype"/>

 </beans> 
Please Note ensure the page beans scope is set to "prototype" so a new \ page instance will be created with every HTTP request. Otherwise Spring will default to using singletons and your code will not be thread safe.

Click instantiated Pages with injected Spring beans and/or ApplicationContext

With this integration option Click will instantiate page instances and automatically inject any page properties which match Spring beans defined in the ApplicationContext.

An example Page class is provided below which has the customerService property automatically injected by the SpringClickServlet. Note the customerService property will need to be defined in a Spring XML configuration.

 package com.mycorp.page;

 import net.sf.click.Page;

 import com.mycorp.service.CustomerService;

 public class CustomerListPage extends Page {

     private CustomerService customerService;

     public void  setCustomerService(CustomerService customerService) {
         this.customerService = customerService;
     }

     ..
 } 
Page property bean name must match the bean name defined in the Spring XML configuration. Continuing our example the Spring XML configuration is provided below:
 <?xml version="1.0" encoding="UTF-8"?>
 <beans>

    <bean id="customerService" class="com.mycorp.service.CustomerService"/>

 </beans> 
This option will also automatically inject the ApplicationContext into new page instances which implement the ApplicationContextAware interface. Using the applicationContext you can lookup Spring beans manually in your pages. For example:
 public class CustomerListPage extends Page implements ApplicationContextAware {

     protected ApplicationContext applicationContext;

     public void setApplicationContext(ApplicationContext applicationContext)  {
         this.applicationContext = applicationContext;
     }

     public CustomerService getCustomerService() {
         return (CustomerService) applicationContext.getBean("customerService");
     }
 } 
This last strategy is probably the least convenient integration option.

Servlet Configuration

The SpringClickServlet can obtain the ApplicationContext either from WebApplicationContextUtils which is configured with a ContextLoaderListener. For example:
 <?xml version="1.0" encoding="UTF-8"?>
 <web-app>

    <listener>
       <listener-class>
          org.springframework.web.context.ContextLoaderListener
       </listener-class>
    </listener>

    <servlet>
       <servlet-name>SpringClickServlet</servlet-name>
       <servlet-class>net.sf.click.extras.spring.SpringClickServlet</servlet-class>
       <load-on-startup>0</load-on-startup>
    </servlet>

    ..

 </web-app> 
Alternatively you can specify the path to the ApplicationContext as a servlet init parameter. For example:
 <?xml version="1.0" encoding="UTF-8"?>
 <web-app>

    <servlet>
       <servlet-name>SpringClickServlet</servlet-name>
       <servlet-class>net.sf.click.extras.spring.SpringClickServlet</servlet-class>
       <init-param>
         <param-name>spring-path</param-name>
         <param-value>/applicationContext.xml</param-value>
       </init-param>
       <load-on-startup>0</load-on-startup>
    </servlet>

    ..

 </web-app> 
To configure page Spring bean injection you need to configure the inject-page-beans servlet init parameter. For example:
 <?xml version="1.0" encoding="UTF-8"?>
 <web-app>

    ..

    <servlet>
       <servlet-name>SpringClickServlet</servlet-name>
       <servlet-class>net.sf.click.extras.spring.SpringClickServlet</servlet-class>
       <init-param>
         <param-name>inject-page-beans</param-name>
         <param-value>true</param-value>
       </init-param>
       <load-on-startup>0</load-on-startup>
    </servlet>

    ..

 </web-app> 

Author:
Malcolm Edgar, Paul Rule, Phil Barnes
See Also:
Serialized Form

Field Summary
protected  ApplicationContext applicationContext
          Spring application context bean factory.
static String INJECT_PAGE_BEANS
          The Servlet initialization parameter name for the option to have the SpringClickServlet inject Spring beans into page instances:   "inject-page-beans".
protected  Map pageSetterBeansMap
          The list of page injectable Spring beans, keyed on page class name.
static String SPRING_PATH
          The Servlet initialization parameter name for the path to the Spring XML appliation context definition file:   "spring-path".
 
Fields inherited from class net.sf.click.ClickServlet
CLICK_FORWARD, CONFIG_SERVICE_CLASS, configService, FORWARD_PAGE, logger, typeConverter
 
Constructor Summary
SpringClickServlet()
           
 
Method Summary
protected  void activatePageInstance(Page page)
          This method associates the ApplicationContext with any ApplicationContextAware pages and supports the deserialized of stateful pages.
protected  ApplicationContext getApplicationContext()
          Return the configured Spring application context.
 void init()
          Initialize the SpringClickServlet and the Spring application context bean factory.
protected  Page newPageInstance(String path, Class pageClass, HttpServletRequest request)
          Create a new Spring Page bean if defined in the application context, or a new Page instance otherwise.
protected  String toBeanName(Class aClass)
          Return the Spring beanName for the given class.
 
Methods inherited from class net.sf.click.ClickServlet
createContext, createControlRegistry, createErrorPage, createPage, createPage, createPage, createPageImports, createTemplateModel, destroy, doGet, doPost, getConfigService, getTypeConverter, handleException, handleRequest, initPage, processPage, processPageOnDestroy, processPageRequestParams, renderJSP, renderTemplate, setPageResponseHeaders, setRequestAttributes
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INJECT_PAGE_BEANS

public static final String INJECT_PAGE_BEANS
The Servlet initialization parameter name for the option to have the SpringClickServlet inject Spring beans into page instances:   "inject-page-beans".

See Also:
Constant Field Values

SPRING_PATH

public static final String SPRING_PATH
The Servlet initialization parameter name for the path to the Spring XML appliation context definition file:   "spring-path".

See Also:
Constant Field Values

applicationContext

protected ApplicationContext applicationContext
Spring application context bean factory.


pageSetterBeansMap

protected Map pageSetterBeansMap
The list of page injectable Spring beans, keyed on page class name.

Constructor Detail

SpringClickServlet

public SpringClickServlet()
Method Detail

init

public void init()
          throws ServletException
Initialize the SpringClickServlet and the Spring application context bean factory. An Spring ClassPathXmlApplicationContext bean factory is used and initialize with the servlet init-param named "spring-path".

Throws:
ServletException - if the click app could not be initialized
See Also:
ClickServlet.init()

newPageInstance

protected Page newPageInstance(String path,
                               Class pageClass,
                               HttpServletRequest request)
                        throws Exception
Create a new Spring Page bean if defined in the application context, or a new Page instance otherwise.

If the "inject-paget-beans" option is enable this method will inject any Spring beans matching the Page's properties.

Parameters:
path - the request page path
pageClass - the page Class the request is mapped to
request - the page request
Returns:
a new Page object
Throws:
Exception - if an error occurs creating the Page
See Also:
ClickServlet.newPageInstance(String, Class, HttpServletRequest)

getApplicationContext

protected ApplicationContext getApplicationContext()
Return the configured Spring application context.

Returns:
the configured Spring application context.

activatePageInstance

protected void activatePageInstance(Page page)
This method associates the ApplicationContext with any ApplicationContextAware pages and supports the deserialized of stateful pages.

Parameters:
page - the page instance to activate
See Also:
ClickServlet.activatePageInstance(Page)

toBeanName

protected String toBeanName(Class aClass)
Return the Spring beanName for the given class.

Parameters:
aClass - the class to get the Spring bean name from
Returns:
the class bean name