webwork / docs / wikidocs / Components.html

        <title>WebWork 2 : Components</title>
	    <link rel="stylesheet" href="styles/site.css" type="text/css" />
        <META http-equiv="Content-Type" content="text/html; charset=UTF-8">	    

	    <table class="pagecontent" border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ffffff">
			    <td valign="top" class="pagebody">
				    <div class="pageheader">
					    <span class="pagetitle">
                            WebWork 2 : Components
				    <div class="pagesubheading">
					    This page last changed on Jun 18, 2004 by <font color="#0050B2">plightbo</font>.

				    <p class="paragraph"><h2 style="margin: 4px 0px 4px 0px;" class="heading2"><a name="Components-Overview"> Overview</a></h2></p>WebWork builds on XWork&#039;s component implementation by providing lifecycle management of component objects and then making these components available to your action classes (or any other user code for that matter) as required. <p class="paragraph">Two types of classes in WebWork can use an enabler interface for inversion of control: Actions and Components. In order for an Action class to have its components set, the ComponentInterceptor must be made available for the Action to set those resources. In turn, if those components require other components to be initialized and set for their own use, those initializations take place at the time the ComponentInterceptor intercepts the action as well.</p><h2 class="heading2"><a name="Components-ScopesandLifecycle">Scopes and Lifecycle</a></h2><p class="paragraph">Components can be configured to exist across three different scopes in WebWork:
<li> for the duration of a single request,</li>
<li> across a user session, or</li>
<li> for the entire lifetime of the web application.</li>
WW:WebWork lazy loads components, meaning that components, no matter what scope, are initialized at the time they are used and disposed of at the end of the given lifecycle of that scope.  Thus, an application scoped component, for example, will be initialized the first time a user makes a request to an action that implements the enabler interface of that component and will be disposed of at the time the application closes.</p>While components are allowed to have dependencies on other components they must not depend on another component that is of a narrower scope. So, for example, a session component cannot depend on a component that is only of request scope.<p class="paragraph">All components must be registered in the components.xml file, which is discussed in the Configuration section.</p><h2 class="heading2"><a name="Components-ObtainingaComponentManager"> Obtaining a ComponentManager</a></h2><p class="paragraph">During any request there are three component managers in existence, one for each scope. They are stored as an attribute called &quot;DefaultComponentManager&quot; in their respective scope objects. So if for example you need to retrieve the ComponentManager object for the request scope, the following code will do the trick:</p><div class="code"><div class="codeContent">
<pre>ComponentManager cm = (ComponentManager) request.getAttribute(<span class="java&#45;quote">&quot;DefaultComponentManager&quot;</span>);</pre>

	    <table border="0" cellpadding="0" cellspacing="0" width="100%">
				<td height="12" background="border/border_bottom.gif"><img src="border/spacer.gif" width="1" height="1" border="0"/></td>
			    <td align="center"><font color="grey">Document generated by Confluence on Oct 15, 2004 02:03</font></td>