WebWork中实现IoC(4)

来源:互联网 发布:知乎日报 最佳吐槽 编辑:程序博客网 时间:2024/06/03 18:05
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

4、在WebWorkXWork中配置组件

1)配置web.xml

要配置WebWork的组件管理,需要在web.xml中加入下面各行:

<filter>
    <filter-name>container</filter-name>
    <filter-class>com.opensymphony.WebWork.lifecycle.RequestLifecycleFilter</filter-class>
</filter>
 
<filter-mapping>
   <filter-name>container</filter-name>
   <url-pattern>*.action</url-pattern> <!-- modify appropriately -->
</filter-mapping>
 
<listener>
    <listener-class>com.opensymphony.WebWork.lifecycle.SessionLifecycleListener</listener-class>
</listener>
 
<listener>
    <listener-class>com.opensymphony.WebWork.lifecycle.ApplicationLifecycleListener</listener-class>
</listener>

这些设置允许WebWorkapplicationsessionrequest范围内管理组件。注意,即使你的应用程序某种范围没有并不需求,也都要包括。

2)配置xwork.xml

ComponentInterceptor用来将IoC模式应用到XWorkAction中。因此,必须在xwork.xml中使用<interceptors>来声明ComponentInterceptor

<interceptor name="component"
        class="com.opensymphony.xwork.interceptor.component.ComponentInterceptor"/>

如果你想应用IoCAction和组件以外的对象,需要直接使用ComponentManager

另外,ComponentInterceptor已经在WebWorkdefaultStack应用。因此,如果你在xwork.xml中应用了defaultStack,就已经包含ComponentInterceptor了。

3)配置components.xml

components.xml用来定义可用的组件。这里指定的组件会被加载到XWorkComponentManager,对成为指定enabler实例的Action有效。components.xml必须放在WEB-INF/classes目录下。

下面的例子在components.xml中配置名为Counter组件,它的生命周期范围是session,将会被传递给实现CounterAware接口的对象:

<components>
    <component>
        <scope>session</scope>
        <class>com.opensymphony.WebWork.example.counter.Counter</class>
        <enabler>com.opensymphony.WebWork.example.counter.CounterAware</enabler>
    </component>
</components>

每个component有三个属性:

l         scope:组件生命周期范围,有效值为applactionsessionrequest

l         class:组件类全路经

l         enablerenabler类或接口(后者推荐使用),任何作为enabler实例的Action,都会被传递组件实例

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击