The Life-Cycle of a Wicket Application
Loading the Application
A Wicket application runs in any J2EE compliant application server by defining a Java filter in the application's
The filter class specified will always be "
org.apache.wicket.protocol.http.WicketFilter". The "
applicationClassName" init-param provided must be the name of a
WebApplication subclass. In the case above, it is "
WicketFilter is loaded, it will use this information to instantiate a single instance of your application class.
Servicing a Request
The following steps occur each time that WicketFilter intercepts a request and a redirect is not required:
- it sets the application to the thread context
- the application creates a
processRequestAndDetach()method is called to handle the request
How RequestCycle Handles a Request
For each request, a
RequestCycle object does the following 3 things:
- Calls the overridable method
RequestCyclesubclasses to do things at the beginning of each request, such as opening a Hibernate session.
- Resolves the
IRequestHandlerto handle the request.
- Calls any listeners to
requestHandlerExecutor.execute(handler);which in turn calls
IRequestHandler.respond(). See API for a list of implementations.
- Calls any listeners to
- Calls the overridable method
RequestCyclesubclasses to do things at the end of each request, such as closing a Hibernate session.
Step 3 may involve some pretty sophisticated logic. When a request URL is parsed, it may include information such as a
Component listener to invoke. This
Component listener may create a whole new Page that is used.
When a Page is replicated from one machine in a cluster to another, the
onSessionAttach() method will be called for every component on the Page.
When models or model values are changed in Wicket by a call to
setModelObject(), the change results in a call to
onModelChanging() before the change actually occurs and then
onModelChanged() after the change occurs.
Because Wicket cannot always know if a model has changed, you can help it to do smart things (such as versioning your Page's model changes)
modelChanging() before altering a Component's model object and modelChanged() afterwards. For example, if your component has a
Model that wraps a List object, there is no way for Wicket to know if you set the 3rd element to some new value. By calling modelChanging() and
modelChanged(), you can ensure that Wicket knows about your change.
Page renders itself by rendering its associated markup (the html file that sits next to the Page). As MarkupContainer (the superclass for Page) iterates through the markup stream for the associated markup, it looks up components attached to the tags in the markup by id. Since the
MarkupContainer (in this case a Page) is already constructed and initialized by onBeginRequest(), the child for each tag should be available in the container. Once the
Component is retrieved, it's render() method is called.
If no component can be retrieved for a given id in the markup stream, a chain of
IComponentResolver objects in the Application is consulted. If one of the resolvers can resolve the id, it may actually add a component to the component hierarchy during rendering. However, such an action is special purpose and internal to Wicket and
IComponentResolver is not intended for use beyond the existing AutoComponentResolver resolver that handles <wicket:component> tags and the AutoLinkResolver which creates automatic page links. The component hierarchy changes that these resolvers make are handled using special internal methods and should not be used as a general extension point for modifying Pages during rendering, which is illegal to do outside of the Wicket core.
Component.render() follows these steps to render a component:
- Determine component visibility. If the component is not visible, the
RequestCycle's Response is changed to
NullResponse.getInstance(), which is a
Responseimplementation that simply discards its output.
Component.onRender()is called to allow the Component's rendering implementation to actually render the Component.
- Any Component models are detached to reduce the size of the Component hierarchy in case it is replicated across a cluster.
The implementation of
onRender() can really be anything that writes to the Response, but it typically is not overridden. The default
MarkupContainer simply calls:
which renders the markup in the container. The implementation of
WebMarkupContainer calls a different method that is tuned to rendering reusable components rather than containers full of arbitrary markup:
renderComponent() method gets a mutable copy of the next
Tag in the markup stream for the component and calls
onComponentTag(Tag) to allow the subclass to modify the tag. Once the subclass has changed the tag, it is written out to the
renderComponentTag(Tag) and the markup stream is advanced to the next tag. Next
onComponentTagBody() is called, passing in the
MarkupStream and the
ComponentTag that was written out as the opening tag. This allows the component to do whatever it needs to do to produce a body for the component tag. One operation the subclass can call in
Component.replaceComponentTagBody(), which replaces the markup inside the component's body with an arbitrary
String. Finally, the framework writes out any appropriate closing tag for the component's open tag.
Rendering to a string
For rendering a complete page to a string, see this article.
For rendering just a
Panel to a string, see Rendering Panel to a String#render-panel-string.