|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.servlet.GenericServlet javax.servlet.http.HttpServlet org.apache.click.ClickServlet org.apache.click.extras.spring.SpringClickServlet
public class SpringClickServlet
Provides a Spring framework integration SpringClickServlet.
This Spring integration servlet provides a number of integration options using Spring with Click pages. These options are detailed below. Stateful pages caveat: please note that stateful pages do not work with all options.customer-list.htm -> com.mycorp.page.CustomerListPage -> customerListPage HTML Request Click Page Class Spring Bean NameWhen using this strategy use the PageScopeResolver class to ensure new Page instances are created with each request, rather than Spring's default "singleton" creation policy. Please see the
PageScopeResolver
Javadoc
for more information on configuring this option.
An example Page class is provided below which uses the Spring @Component annotation.
Note in this example page the customerService with the @Resource
annotation is injected by Spring after the page instance has been instantiated.
package com.mycorp.page; import javax.annotation.Resource; import org.apache.click.Page; import org.springframework.stereotype.Component; import com.mycorp.service.CustomerService; @Component public class CustomerListPage extends Page { @Resource(name="customerService") private CustomerService customerService; .. }This is the most powerful and convenient Spring integration option, but does require Spring 2.5.x or later. Stateful page caveat: Spring beans injected on stateful pages will be serialized along with the page, meaning those beans must implement the Serializable interface. If you do not want the beans to be serialized, they need to be marked as transient. Transient beans won't be serialized but when the page is deserialized, the transient beans won't be re-injected, causing a NullPointerException when invoked. If you want to use transient beans on stateful pages, see option 3 below.
customer-list.htm -> com.mycorp.page.CustomerListPage -> customerListPage HTML Request Click Page Class Spring Bean NameIf 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 NameThis 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 or later. 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. Stateful page caveat: option 2 has the same caveat as option 1.
package com.mycorp.page; import org.apache.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 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.
package com.mycorp.page; import org.apache.click.Page; import com.mycorp.service.CustomerService; public class CustomerListPage extends Page implements ApplicationContextAware { // Note the transient keyword private transient CustomerService customerService; protected transient ApplicationContext applicationContext; public CustomerListPage { // Page is marked as stateful setStateful(true); } // Inject the customer service public void setCustomerService(CustomerService customerService) { this.customerService = customerService; } public CustomerService getCustomerService() { return (CustomerService) applicationContext.getBean("customerService"); } // Inject Spring's ApplicationContext public void setApplicationContext(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } .. }
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>org.apache.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>org.apache.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 (option 3 above), 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>org.apache.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>
PageScopeResolver
,
Serialized FormField 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<Class<? extends Page>,List<org.apache.click.extras.spring.SpringClickServlet.BeanNameAndMethod>> |
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 application context definition file: "spring-path". |
Fields inherited from class org.apache.click.ClickServlet |
---|
CLICK_FORWARD, CONFIG_SERVICE_CLASS, configService, FORWARD_PAGE, logger, memberAccess, resourceService, TYPE_CONVERTER_CLASS, typeConverter |
Constructor Summary | |
---|---|
SpringClickServlet()
|
Method Summary | |
---|---|
protected void |
activatePageInstance(Page page)
This method associates the ApplicationContext with any ApplicationContextAware pages and supports the deserialization 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<? extends Page> 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 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 |
---|
public static final String INJECT_PAGE_BEANS
public static final String SPRING_PATH
protected ApplicationContext applicationContext
protected Map<Class<? extends Page>,List<org.apache.click.extras.spring.SpringClickServlet.BeanNameAndMethod>> pageSetterBeansMap
Constructor Detail |
---|
public SpringClickServlet()
Method Detail |
---|
public void init() throws ServletException
init
in class ClickServlet
ServletException
- if the click app could not be initializedClickServlet.init()
protected Page newPageInstance(String path, Class<? extends Page> pageClass, HttpServletRequest request) throws Exception
newPageInstance
in class ClickServlet
path
- the request page pathpageClass
- the page Class the request is mapped torequest
- the page request
Exception
- if an error occurs creating the PageClickServlet.newPageInstance(String, Class, HttpServletRequest)
protected ApplicationContext getApplicationContext()
protected void activatePageInstance(Page page)
activatePageInstance
in class ClickServlet
page
- the page instance to activateClickServlet.activatePageInstance(Page)
protected String toBeanName(Class<?> aClass)
aClass
- the class to get the Spring bean name from
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |