简介
我见过许多项目开发者实现自己专有的mvc框架。这些开发者并不是因为想实现不同于struts的某些功能,而是还没有意识到怎么去扩展struts。通过开发自己的mvc框架,你可以掌控全局,但同时这也意味着你必须付出很大的代价;在项目计划很紧的情况下也许根本就不可能实现。
struts不但功能强大也易于扩展。你可以通过三种方式来扩展struts:
1.plugin:在应用启动或关闭时须执行某业务逻辑,创建你自己的plugin类
2.requestprocessor:在请求处理阶段一个特定点欲执行某业务逻辑,创建你自己的requestprocessor。例如:你想继承requestprocessor来检查用户登录及在执行每个请求时他是否有权限执行某个动作。
3.actionservlet:在应用启动或关闭或在请求处理阶段欲执行某业务逻辑,继承actionservlet类。但是必须且只能在pligin和requestprocessor都不能满足你的需求时候用。
本文会列举一个简单的struts应用来示范如何使用以上三种方式扩展struts。在本文末尾资源区有每种方式的可下载样例源代码。struts validation 框架和 tiles 框架是最成功两个的struts扩展例子。
我是假设读者已经熟悉struts框架并知道怎样使用它创建简单的应用。如想了解更多有关struts的资料请参见资源区。
plugin
根据struts文档,“plugin是一个须在应用启动和关闭时需被通知的模块定制资源或服务配置包”。这就是说,你可以创建一个类,它实现plugin的接口以便在应用启动和关闭时做你想要的事。
假如创建了一个web应用,其中使用hibernate做为持久化机制;当应用一启动,就需初始化hinernate,这样在web应用接收到第一个请求时,hibernate已被配置完毕并待命。同时在应用关闭时要关闭hibernate。跟着以下两步可以实现hibernate plugin的需求。
1.创建一个实现plugin接口的类,如下:
public class hibernateplugin implements plugin{ private string configfile; // this method will be called at application shutdown time public void destroy() { system.out.println("entering hibernateplugin.destroy()"); //put hibernate cleanup code here system.out.println("exiting hibernateplugin.destroy()"); } //this method will be called at application startup time public void init(actionservlet actionservlet, moduleconfig config) throws servletexception { system.out.println("entering hibernateplugin.init()"); system.out.println("value of init parameter " + getconfigfile()); system.out.println("exiting hibernateplugin.init()"); } public string getconfigfile() { return name; } public void setconfigfile(string string) { configfile = string; }}
实现plugin接口的类必须是实现以下两个方法:
init() 和destroy().。在应用启动时init()被调用,关闭destroy()被调用。struts允许你传入初始参数给你的plugin类;为了传入参数你必须在plugin类里为每个参数创建一个类似javabean形式的setter方法。在hibernateplugin类里,欲传入configfile的名字而不是在应用里将它硬编码进去
2.在struts-condig.xml里面加入以下几行告知struts这个新的plugin
<struts-config> ... <!-- message resources --> <message-resources parameter= "sample1.resources.applicationresources"/> <!-- declare your plugins --> <plug-in classname="com.sample.util.hibernateplugin"> <set-property property="configfile" value="/hibernate.cfg.xml"/> </plug-in></struts-config>
classname属性是实现plugin接口类的全名。为每一个初始化传入plugin类的初始化参数增加一个<set-property>元素。在这个例子里,传入config文档的名称,所以增加了一个config文档路径的<set-property>元素。
tiles和validator框架都是利用plugin给初始化读入配置文件。另外两个你还可以在plugin类里做的事情是:
假如应用依赖于某配置文件,那么可以在plugin类里检查其可用性,假如配置文件不可用则抛出servletexception。这将导致actionservlet不可用。
plugin接口的init()方法是你改变moduleconfig方法的最后机会,moduleconfig方法是描述基于struts模型静态配置信息的集合。一旦plugin被处理完毕,struts就会将moduleconfig冻结起来。
请求是如何被处理的
actionservlet是struts框架里唯一一个servlet,它负责处理所有请求。它无论何时收到一个请求,都会首先试着为现有请求找到一个子应用。一旦子应用被找到,它会为其生成一个requestprocessor对象,并调用传入httpservletrequest和httpservletresponse为参数的process()方法。
大部分请处理都是在requestprocessor.process()发生的。process()方法是以模板方法(template method)的设计模式来实现的,其中有完成request处理的每个步骤的方法;所有这些方法都从process()方法顺序调用。例如,寻找当前请求的actionform类和检查当前用户是否有权限执行action mapping都有几个单独的方法。这给我们提供了极大的弹性空间。struts的requestprocessor对每个请求处理步骤都提供了默认的实现方法。这意味着,你可以重写你感兴趣的方法,而其余剩下的保留默认实现。例如,struts默认调用request.isuserinrole()检查用户是否有权限执行当前的actionmapping,但如果你需要从数据库中查找,那么你要做的就是重写processroles()方法,并根据用户角色返回true 或 false。
首先我们看一下process()方法的默认实现方式,然后我将解释requestprocessor类里的每个默认的方法,以便你决定要修改请求处理的哪一部分。
public void process(httpservletrequest request, httpservletresponse response) throws ioexception, servletexception { // wrap multipart requests with a special wrapper request = processmultipart(request); // identify the path component we will // use to select a mapping string path = processpath(request, response); if (path == null) { return; } if (log.isdebugenabled()) { log.debug("processing a '" + request.getmethod() + "' for path '" + path + "'"); } // select a locale for the current user if requested processlocale(request, response); // set the content type and no-caching headers // if requested processcontent(request, response); processnocache(request, response); // general purpose preprocessing hook if (!processpreprocess(request, response)) { return; } // identify the mapping for this request actionmapping mapping = processmapping(request, response, path); if (mapping == null) { return; } // check for any role required to perform this action if (!processroles(request, response, mapping)) { return; } // process any actionform bean related to this request actionform form = processactionform(request, response, mapping); processpopulate(request, response, form, mapping); if (!processvalidate(request, response, form, mapping)) { return; } // process a forward or include specified by this mapping if (!processforward(request, response, mapping)) { return; } if (!processinclude(request, response, mapping)) { return; } // create or acquire the action instance to // process this request action action = processactioncreate(request, response, mapping); if (action == null) { return; } // call the action instance itself actionforward forward = processactionperform(request, response, action, form, mapping); // process the returned actionforward instance processforwardconfig(request, response, forward); }
1、processmultipart(): 在这个方法中,struts读取request以找出contenttype是否为multipart/form-data。假如是,则解析并将其打包成一个实现httpservletrequest的包。当你成生一个放置数据的html form时,request的contenttype默认是application/x-www-form-urlencoded。但是如果你的form的input类型是file-type允许用户上载文件,那么你必须把form的contenttype改为multipart/form-data。如这样做
闽公网安备 35060202000074号