Source

webwork / docs / components.html

Full commit
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <title>OpenSymphony Wiki (Offline Version) :: WebWork2 Documentation</title>
  <link type="text/css" href="main.css" rel="STYLESHEET"/>
</head>
<body>
  <div id="page-logo">
    <a href="index.html">WebWork2 Documentation</a>
  </div>
    <div class="snip-title">
	  <h1 class="snip-name">WebWork 2 Components
  
  </h1>
  </div>
<div id="snip-content" class="snip-content">

 <div class="snip-attachments"></div>
 
 <h3 class="heading-1">Overview
</h3><p class="paragraph"/><a href="index.html">WebWork2</a> builds on <a href="http://wiki.opensymphony.com/space/XWork">XWork</a>'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"/>Components can be configured to exist across three different scopes in WebWork:
<ol>
<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>
</ol><p class="paragraph"/>This document shows you how to configure WebWork to <i class="italic">use</i> component objects. For details on creating your own components, see <a href="http://wiki.opensymphony.com/space/XWork+Components">XWork Components</a>.
<h3 class="heading-1">Configuration - web.xml
</h3><p class="paragraph"/>To configure WebWork's component management, the following lines must be added in the appropriate places to web.xml:<p class="paragraph"/><div class="code"><pre>&#60;filter&#62;
    &#60;filter&#45;name&#62;container&#60;/filter&#45;name&#62;
    &#60;filter&#45;class&#62;com.opensymphony.webwork.lifecycle.RequestLifecycleFilter&#60;/filter&#45;class&#62;
&#60;/filter&#62;<p class="paragraph"/>&#60;listener&#62;
    &#60;listener&#45;class&#62;com.opensymphony.webwork.lifecycle.SessionLifecycleListener&#60;/listener&#45;class&#62;
&#60;/listener&#62;<p class="paragraph"/>&#60;listener&#62;
    &#60;listener&#45;class&#62;com.opensymphony.webwork.lifecycle.ApplicationLifecycleListener&#60;/listener&#45;class&#62;
&#60;/listener&#62;</pre></div><p class="paragraph"/>These settings allow WebWork to manage components across the application, session and request scopes. Note that even if one or more of the scopes are not required by your application, all three scopes need to be specified in web.xml for WebWork's component management to function correctly.
<h3 class="heading-1">Configuration - xwork.xml
</h3><p class="paragraph"/>The ComponentInterceptor is used to supply components to your actions. See <a href="http://wiki.opensymphony.com/space/XWork+Components">XWork Components</a> for details on configuring the ComponentInterceptor in xwork.xml.
<h3 class="heading-1">Configuration - components.xml
</h3><p class="paragraph"/>The components.xml file is used to specify the components that are to be available. The components specified here are loaded into XWork's ComponentManager and are then made available to any actions that are an instance of the specified enabler. The components.xml file must be placed in the root of the classpath (ie, in the WEB-INF/classes directory).<p class="paragraph"/>Here is an example components.xml file that configures a Counter component. The Counter object will live in session scope, and will be passed to any objects that are enabled due to their implementing the CounterAware interface:<p class="paragraph"/><div class="code"><pre>&#60;components&#62;
    &#60;component&#62;
        &#60;scope&#62;session&#60;/scope&#62;
        &#60;class&#62;com.opensymphony.webwork.example.counter.Counter&#60;/class&#62;
        &#60;enabler&#62;com.opensymphony.webwork.example.counter.CounterAware&#60;/enabler&#62;
    &#60;/component&#62;
&#60;/components&#62;</pre></div><p class="paragraph"/>Each component must have the following three attributes:
<ul class="star">
<li><b class="bold">scope</b>: Valid values are <i class="italic">application</i>, <i class="italic">session</i> and <i class="italic">request</i>. This determines the component's lifetime. Application scope components will be created when the webapp starts up, and they will survive for the whole lifetime of the webapp. Session scoped components exist for the duration of a user session, while components in request scope only last for the duration of a single client request.</li>
<li><b class="bold">class</b>: This specifies the component's class. An instance of this object will live for the duration of the specified scope, and will be made available to any actions (or other code) as required. Note that components are lazy-loaded, so if nothing makes use of the component during its lifetime, the component will never actually be instantiated. At the moment components must have a zero argument constructor.</li>
<li><b class="bold">enabler</b>: Any actions that are an instanceof the enabler class or interface will be passed an instance of the component.</li>
</ul><p class="paragraph"/>Note that 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.
<h3 class="heading-1">Obtaining a ComponentManager
</h3><p class="paragraph"/>During any request there are three component managers in existence, one for each scope. They are stored as an attribute called "DefaultComponentManager" 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 class="paragraph"/><div class="code"><pre>ComponentManager cm = (ComponentManager) request.getAttribute(<span class="java&#45;quote">"DefaultComponentManager"</span>);</pre></div>
  </div>
</body>
</html>