Struts 2.2.3.1指南:插件开发指南

第3章 插件开发指南
Apache的Struts 2提供了一个简单的插件架构,因此开发人员只需要把一个JAR文件添加到应用程序的classpath中就可以扩展框架。因为插件包含在一个JAR文件中,因此它们易于与他人共享。有些插件是与框架捆绑在一起的,而其它插件可以从第三方的资源中获取。

捆绑的插件
(1)Codebehind插件(已废弃)
(2)Config Browser插件
(3)Convention插件(2.1.3+)
(4)Javatemplates插件(2.1.3+)
(5)OVal插件(2.1.7+)
(6)OSGi插件(2.1.7+)
(7)JSON插件(2.1.7+)
(8)Embedded JSP插件(2.1.7+)
(9)JSF插件
(10)Plexus插件
(11)Portlet插件
(12)DWR插件
(13)REST插件(2.1.1+)
(14)SiteGraph插件
(15)SiteMesh插件
(16)Spring插件
(17)Struts 1插件
(18)Tiles插件
(19)JUnit插件
(20)TestNG插件
(21)JFreeChart插件
(22)JasperReports插件

关于捆绑的和第三方插件的完整列表,请访问插件注册表。
3.1 插件
Struts 2插件包括扩展、替换、或添加现有Struts框架功能的类和配置。一个插件可以通过把一个JAR文件添加到应用程序的类路径来安装,除JAR文件之外,还要满足插件本身可能有的所有依赖。要配置插件,JAR文件必须包含一个struts-plugin.xml文件,此文件遵循与普通的struts.xml文件一样的格式。

因为插件可以包含一个struts-plugin.xml文件,因此它具有以下功能:
(1)定义包含result、拦截器、action的新包;
(2)覆盖框架的常量;
(3)引入新的扩展点的实现类。

框架的很多通用的但是可选的功能都是作为插件发布的。一个应用程序可以保留发布包提供的所有插件,或者只是包括它所使用的插件。插件可以用于组织应用程序的代码,或把代码发布给第三方。

插件不会以任何特殊的顺序来加载。插件不应该相互依赖。插件可以依赖Struts核心提供的类,但是它不应该依赖其它插件加载的类。

框架首先会加载它自己的默认配置,然后加载在classpath的其它JAR文件中找到的任何插件配置文件,最后加载struts.xml引导程序。
(1)struts-default.xml(核心JAR文件中附带的);
(2)struts-plugin.xml(在其它JAR文件中可以找到的);
(3)struts.xml(应用程序提供的)。

因为总是最后加载struts.xml文件,因此它可以使用发布包自带的插件或应用程序可以获取的其它插件提供的任何资源。
3.1.1 静态资源
要在插件中包含静态资源,需要把它们添加到jar文件的/static目录下,并且要在页面中使用/static路径来引用静态资源,如以下示例:
Assuming /static/main.css is inside a plugin jar, to add it to the page:
<@s.url value="/static/main.css" var="css" />
<link rel="stylesheet" type="text/css" href="%{#css}" />
3.1.2 扩展点
扩展点允许一个插件使用一个替代实现来覆盖Struts框架中的关键类。例如,一个插件可以提供一个新的类来创建Action类或者把请求映射到Action。

在Struts 2中可以获得以下扩展点:


3.1.3 插件示例
让我们来看核心发布包自带的两个类似的却又不不同的插件。

Sitemesh插件
SiteMesh是一个受欢迎的Tiles替代品。通过在一个普通页面上自动包装一致的元素,如标题和菜单栏,SiteMesh为应用程序的页面提供了一致的观感。sitemesh-plugin.jar包含了一些类、一个标准的JAR清单文件、以及一个插件配置文件。
+ META-INF/
    + manifest.mf
+ org
    + apache
        + struts2
            + sitemesh
                + FreeMarkerPageFilter.class
                + TemplatePageFilter.class
                + VelocityPageFilter.class
+ struts-plugin.xml
而SiteMesh插件没有提供任何新的result、拦截器、或action、或扩展任何Struts集成点,它只需要知道Struts框架中启用了哪些设置。因此,它的struts-plugin.xml文件看起来像这样:
<?xml version="1.0" encoding="UTF-8" ?>
<!--
/*
 * $Id$
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
-->
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <bean class="org.apache.struts2.sitemesh.FreemarkerPageFilter" static="true"
        optional="true"/>
    <bean class="org.apache.struts2.sitemesh.VelocityPageFilter" static="true" optional="true"/>
</struts>
这两个启用了static标识的Bean元素告诉Struts,在启动时要把当前的设置和框架对象注入到静态属性setter中。这样就可以,例如,让FreeMarkerPageFilter类获得Struts的一个FreemarkerManager实例和当前的编码设置。

Tiles插件
Tiles是一个受欢迎的SiteMesh替代品。通过把一个页面分割为几个一致的片段或碎片,Tiles为应用程序的页面提供了一致的观感。tiles-plugin.jar包含了一些类、一个标准的JAR清单文件、以及一个插件配置文件。
+ META-INF/
    + manifest.mf
+ org
    + apache
        + struts2
            + tiles
                + StrutsTilesListener.class
                + StrutsTileUtilImpl.class
            + views
                + tiles
                    + TilesResult.class
+ struts-plugin.xml
因为Tiles插件需要注入配置元素、一个result类,因此它提供了一个struts-plugin.xml文件。
3.1.4 插件注册表
关于捆绑的和第三方插件的更多明细信息,请访问Apache Struts的插件注册表。
3.2 通过自定义插件扩展应用程序
请查看教程中通过自定义插件扩展应用程序的章节。

猜你喜欢

转载自hanyuan8407.iteye.com/blog/1765968