MAP | PageMixer Documents > Tutorial > Mixing with Servlet > Create 'ConsumerContext' | << | >> |
This section explains
how to customize creating ConsumerContext
with PageServlet
.
You can skip this section if you have no interest in context customization(many cases are so).
Class diagram in this section is shown below:
Classes which you must define are colored, and other are already defined.
In this tutorial, abbreviated class names are used. Complete names are shown below.
Notation | Full name |
---|---|
ConsumerContext | jp.ne.dti.lares.foozy.pagemixer.mixer.ConsumerContext |
ContextFactory | jp.ne.dti.lares.foozy.pagemixer.servlet.ContextFactory |
DataProvider | jp.ne.dti.lares.foozy.pagemixer.mixer.DataProvider |
Filter | jp.ne.dti.lares.foozy.pagemixer.mixer.Filter |
ListDataProvider | jp.ne.dti.lares.foozy.pagemixer.mixer.ListDataProvider |
PageServlet | jp.ne.dti.lares.foozy.pagemixer.servlet.PageServlet |
RequestKey | jp.ne.dti.lares.foozy.pagemixer.servlet.RequestKey |
ServletConsumerContext | jp.ne.dti.lares.foozy.pagemixer.servlet.ServletConsumerContext |
ServletUtil | jp.ne.dti.lares.foozy.pagemixer.servlet.ServletUtil |
StrutsConsumerContext | jp.ne.dti.lares.foozy.pagemixer.struts.StrutsConsumerContext |
Notation | Full name |
---|---|
CustomContextFactory | pagemixer.servlet.CustomContextFactory |
Customization of creating ConsumerContext
allows you, per request, to:
ConsumerContext
Good example of the former is the co-operation of PageMixer and Struts, demostration of it is included in src/bin distribution of PageMixer.
For co-operation with Struts,
PageMixer provides "StrutsConsumerContext
"
derived from "ServletConsumerContext
",
and the class to customize ConsumerContext
creation.
Customization for the later may reduce cost to create complex data by preprocessing.
For example,
assumption is that you want (to use the Filter
)
to process the list of Locale
s
which are created from HTTP request header.
It is not efficient to build the list of Locale
s up on-demand,
because how many times processing may be occured depends on
graphical design of HTML page.
Customization in the later way seems to satisfy your needs in almost all cases.
ContextFactory
PageMixer provides
"ContextFactory
" for
customization of creating ConsumerContext
.
In almost all cases,
you can satisfy your needs
by overriding create
method of it,
event though it has more overridable methods for complex purpose.
Please see API document for detail about them.
public ConsumerContext create(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response) throws IOException
Sample overriding is shown below.
// use context created by base class ConsumerContext context = super.create(servlet, request, response); // setup list of locale if(null == context.getValue(KEY_LOCALE_ENTRY_PROVIDER)){ ListDataProvider localeProvider = new ListDataProvider(); Enumeration locales = request.getLocales(); while(locales.hasMoreElements()){ Object locale = locales.nextElement(); localeProvider.add(locale); } context.setValue(KEY_LOCALE_ENTRY_PROVIDER, localeProvider); } // setup "href" attribute value of "base" tag if(null == context.getValue(KEY_BASE_HREF)){ String baseHref = ServletUtil.getBaseHref(context, true, true); context.setValue(KEY_BASE_HREF, baseHref); } return context;
Above sample has two specific customizations.
DataProvider
" of Locale
s:DataProvider
up and
stores it into context by key "KEY_LOCALE_ENTRY_PROVIDER
".
ServletUtil#getBaseHref()
and
stores it into context by key "KEY_BASE_HREF
".
In this example,
KEY_LOCALE_ENTRY_PROVIDER
and
KEY_BASE_HREF
are
instances of RequestKey
,
because scope of both values is limited in request.
NOTE:
In above example,
Former "true" of ServletUtil#getBaseHref()
means
"use HTTP HOST header value if specified".
Later "true" maens "use Servlet context path instead of requested URI". Later allows you to specify location of local resource relatively to Servlet context in response HTML page.
ContextFactory
does not requires any
initialization parameters.
To use your custom ContextFactory
,
you should specify "servlet.contextFactory
"
Servlet init-param in deployment descriptor
as shown below.
<init-param> <param-name> servlet.contextFactory </param-name> <param-value> pagemixer.servlet.CustomContextFactory </param-value> </init-param>
MAP | PageMixer Documents > Tutorial > Mixing with Servlet > Create 'ConsumerContext' | << | >> |