Note | ||
---|---|---|
| ||
At the risk of being unoriginal, request processing in general does two things: changes some data which is stored in HttpSession, database or somewhere else; creates some output which is usually HTML. These is no single class in Wicket that does all the request processing & classes delegate the work to each other. In a simplified way it goes like this:
Panel | ||||
---|---|---|---|---|
| ||||
- browser requests a URL
- WicketFilter/Servlet receives request from servlet container and creates RequestCycle object
- RequestCycle goes through several states, calling at it state certain method in IRequestCycleProcessor
- Depending on request parameters IRequestCycleProcessor creates IRequestTarget (in Wicket this process is referred to as "resolving"). At this stage requested URL is translated into IRequestTarget which already contains requested Page/Component or knows how to create it.
- IRequestTarget use existing Page or creates a new one, optionally asks Components to execute callbacks (user code which handles link clicks, form submits, etc.), then asks Page and Components to render themselves
- Page/Components execute callbacks and render themselves. Callbacks change state and rendering produce markup and write it to HttpResponse. This is the place where most of the user code resides.
- servlet container writes output to browser
There are more classes involved in request processing. Some of them are shown in the diagram below. The diagram is followed by short descriptions of each of these classes. Descriptions say what is the purpose of the class, when it is created and how to get its instance.
It's also a good idea to take a look at these classes' javadocs.
Panel | ||||||
---|---|---|---|---|---|---|
| ||||||
WicketFilter/Servlet
What for
Creates and initializes application. Asks application to create request, response and request cycle objects. Starts request processing by calling request() method on RequestCycle.
Anchor | ||||
---|---|---|---|---|
|
WebApplication
What for
Application and WebApplication can be used to set up a lot of different things which in general may be called configuration. Most frequently used probably are:
- numerous get*Settings() methods which return modifiable object with specific settings. For full list of all possible settings see list of implemented interfaces in the Settings class.
- mount*() methods which basically make page or resource accessible by specified URL
Application and WebApplication also have some overridable methods:
- init() and onDestroy() methods. init() method is called once when WicketFilter/Servlet is created; it should be used for any application setup instead of the constructor. onDestroy() method is called once on filter cleanup.
- numerous new*() methods which create instance of specified class. They include methods for creating RequestCycle, IRequestCycleProcessor, WebSession, ISessionStore, WebRequest, WebResponse (what means that application creates all these classes). You can override any of these methods to make Wicket use you implementation.
Besides that application can be used for storing any user-defined application scope data.
When created
WebApplication is created when WicketFilter/Servlet is initialized. It is created by IWebApplicationFactory implementation. Factory can be specified in WicketFilter/Servlet configuration in the web.xml using applicationFactoryClassName parameter like this:
Code Block | ||||
---|---|---|---|---|
| ||||
<init-param> <param-name>applicationFactoryClassName</param-name> <param-value>org.apache.wicket.MyApplicationFactory</param-value> </init-param> |
You can implement IWebApplicationFactory which will create the application instance you want but usually it is not necessary. If there is no applicationFactoryClassName parameter in the web.xml, Wicket uses default factory implementation which reads applicationClassName parameter and creates application instance of the specified class using reflection.
How to get
There is only one instance of Application per WicketFilter/Servlet. You can get this instance by calling static Application#get() method.
Anchor | ||||
---|---|---|---|---|
|
RequestCycle
What for
The main responsibility of RequestCycle is instructing IRequestCycleProcessor which steps of request handling to execute. For more information see Request cycle and request cycle processor.
When created
New instance of RequestCycle is created on every request. This is a pseudo code of how it happens:
Code Block | ||||
---|---|---|---|---|
| ||||
class WicketServlet ... public final void doGet(...) { ... // WebApplication instance is already created while initializing servlet/filter final WebRequest request = webApplication.newWebRequest(servletRequest); final WebResponse response = webApplication.newWebResponse(servletResponse); RequestCycle cycle = webApplication.newRequestCycle(request, response); cycle.request(); // <--- request processing starts ... } |
How to get
Call RequestCycle.get()
.
Anchor | ||||
---|---|---|---|---|
|
IRequestCycleProcessor
What for
IRequestCycleProcessor gets instructions from RequestCycle and depending on it:
- resolves (creates) IRequestTarget. At this step IRequestCycleProcessor finds out which Page/Component was requested and creates IRequestTarget which knows about it. In case of requesting stateful previously accessed page IRequestCycleProcessor asks WebSession to get this page instance.
- asks IRequestTarget to process events
- asks IRequestTarget to create response
For more information see Request cycle and request cycle processor.
When created
There is one instance of IRequestCycleProcessor per Application. It is lazily created during the first request. Its creation can be customized by overriding WebApplication#newRequestCycleProcessor().
How to get
Use WebApplication#getRequestCycleProcessor() method (you shouldn't need to do it).
Anchor | ||||
---|---|---|---|---|
|
IRequestCodingStrategy
What for
In short IRequestCodingStrategy is responsible for converting URL to object representation and vice versa. It "decodes" incoming request and creates a RequestParameters object which is then passed to IRequestCycleProcessor to resolve target. IRequestCodingStrategy also has in some sense reverse method which "encodes" request targets, what means creating a URL for specified IRequestTarget.
Code Block | ||||
---|---|---|---|---|
| ||||
public interface IRequestCodingStrategy ... RequestParameters decode(Request request); CharSequence encode(RequestCycle requestCycle, IRequestTarget requestTarget); |
See also Request coding strategy page.
When created
There is one instance of IRequestCodingStrategy per IRequestCycleProcessor which is lazily created during the first request. Its creation can be customized by overriding AbstractRequestCycleProcessor#newRequestCodingStrategy().
How to get
Get IRequestCycleProcessor instance and use IRequestCycleProcessor#getRequestCodingStrategy() method.
Anchor | ||||
---|---|---|---|---|
|
IRequestTarget
What for
IRequestTarget contains Page/Component or knows how to create it. It is responsible for calling event handling code (like Form#onSubmit() method) and asking pages and components to render.
When created
IRequestTarget is created by IRequestCycleProcessor on every request and stored in RequestCycle. Which subclass of IRequestTarget will be created depends on RequestParameters instance which is decoded from requested URL. For example if a bookmarkable page is requested, BookmarkablePageRequestTarget is created; if a link is pressed, ListenerInterfaceRequestTarget is created.
See also Request targets page.
How to get
You can get current request target from by getting RequestCycle instance and calling RequestCycle#getRequestTarget() method.
Anchor |
---|
WebRequest, WebResponse
What for
Basically WebRequest and wrap HttpServletRequest and HttpServletResponse classes. WebResponse is also used in components as an output for markup.
When created
They are created on every request by Application just before creating RequestCycle. See description for RequestCycle.
How to get
Get RequestCycle instance, use RequestCycle#getRequest/Response() method and cast returned object if exactly WebRequest/Response is needed.
Anchor | ||||
---|---|---|---|---|
|
WebSession
What for
WebSession holds information about a user session including some fixed number of most recent pages. WebSession roughly corresponds to HttpSession.
When created
It is lazily created during request handling. Then WebSession is stored in HttpSession and restored from there on next request. If HttpSession expires and WebSession in which is stored it can't be reached anymore new WebSession instance is created. To customize Session creation you can override Application#newSession() method.
How to get
You can get instance of Session using static Session#get() method.
Anchor |
---|
ISessionStore
What for
"The actual store that is used by Session to store its attributes". It means that Session doesn't necessarily store data in HttpSession.
When created
There is one instance of ISessionStore which is created at Application creation.
How to get
Use Application#getSessionStore() method (you shouldn't need to do it).
Anchor |
---|
Page, Component
What for
Pages and Components are perhaps the most important part of all. It is the place where most of the user code resides. You should be able to do most of the things you may want to do in web-application without knowing anything else apart from Pages and Components (and perhaps a little bit about Application).
Components generally do three things:
have state, which is state that components in rich GUI application may have. Wicket automatically stores into Session all visited Pages and therefore all nested Components and their state.
provide callbacks, which are basically any user-specified code
render themselves, what means writing some tags to WebResponse. See also Render strategies.
See also Component hierarchy.
When created
Components are created "manually". Usually it happens in a page constructor.
Pages are created:
every time when bookmarkable page is requested. This includes application home page which must be bookmarkable. Bookmarkable pages are created by implementation of IPageFactory (see ISessionSettings#get/setPageFactory() method). See also Bookmarkable pages and links, Pages.
whenever page is explicitly created as a normal java object.