iBatis的JpetStore示例中MVC机制实现的研究--转

JpetStore中的Action与普通Struts的Action处理方式不一样。遍历JpetStore的src文件夹,并无一个具体的Action,那么它是如何来完成普通Struts的Action工作了?
查看JpetStore的Struts.xml可以发现,它的Action只有一个,即“org.apache.stuts.beanaction.Beanaction”。通过Eclipse查看beanaction.jar的源代码,可以发现Beanaction继承与普通Action,即具备普通的action的功能。那么它无具体Action的奥妙在哪,继续研究BeanAction的代码,截取BeanAction的excute方法中核心部分代码如下:
/**//*遍历此方法的业务逻辑可知,*的优先级最高,如果是*,则不调用任何方法直接Forward,类似于ForwardAction*/
private static final String NO_METHOD_CALL = "*";
…….
/**//*所有的FormBean都继承于BaseBean*/
BaseBean bean = (BaseBean) form;
ActionContext.initCurrentContext(request, response);
if (bean != null) {
// Explicit Method Mapping
/**//*下面是检查struts.xml配置中是否有parameter属性*/
Method method = null;
String methodName = mapping.getParameter();
if (methodName != null && !NO_METHOD_CALL.equals(methodName)) {
try {
/**//*通过反射,根据得到的方法名称取得方法的句柄*/
method = bean.getClass().getMethod(methodName, null);
synchronized (bean) {
/**//*下面是关键一句,调用basebean拥有的接口ActionInterceptor的实现DefaultActionInterceptor,来完成具体方法的调用*/
forward = bean.getInterceptor().intercept(new ActionInvoker(bean, method));
}
……..
/**//*无Parameter属性,检查path路径的最后一个/后的名称,即为调用的方法名*/
// Path Based Method Mapping
if (method == null && !NO_METHOD_CALL.equals(methodName)) {
methodName = mapping.getPath();
if (methodName.length() > 1) {
int slash = methodName.lastIndexOf("/") + 1;
methodName = methodName.substring(slash);
if (methodName.length() > 0) {
try {
method = bean.getClass().getMethod(methodName, null);
synchronized (bean) {
forward = bean.getInterceptor().intercept(new ActionInvoker(bean, method));
}
……..
/**//*根据调用方法返回的String,得到页面的转移路径*/
return mapping.findForward(forward);

通过研究上面这段代码,我们可知,JpetStore中没有具体Action实现的关键原因即在于下面这几句
/**//*通过反射,根据得到的方法名称取得方法的句柄*/
method = bean.getClass().getMethod(methodName, null);
synchronized (bean) {
/**//*下面是关键一句,调用basebean拥有的接口ActionInterceptor的实现DefaultActionInterceptor,来完成具体方法的调用*/
forward = bean.getInterceptor().intercept(new ActionInvoker(bean, method));
}
即将原来Action中的excute方法的实现转移到FormBean中,这样实现显得更为简捷,方便。研究ActionInvoke,它的核心代码如下:
public String invoke() {
try {
return (String) method.invoke(bean, null);
} catch (Exception e) {
throw new BeanActionException("Error invoking Action. Cause: " + e, e);
}
}
至此可知,它调用的是formbean中的函数。且从这段代码可知,formbean的这类特殊函数,此处称为action方法,要符合两个特征:1)无参数;2)返回值为string,此返回string即是Struts-config.xml的全局或局部的forward。
以上是整个beanaction的实现机制。个人感觉此种实现方法对于开发者而言已经类似于ASP.NET的.aspx与.cs开发模式了。下面是通过实例来说明一下BeanAction如何控制formbean的

Struts-config.xml的配置里有3种映射方式,来告诉BeanAction把控制转到哪个form bean对象的哪个方法来处理。
(1)parameter=”*’直接跳转;(2)Parameter中含具体的方法名;(3)Path中最后一个/后的方法名
以这个请求连接为例http://localhost/jpetstore4/shop/viewOrder.shtml
1. URL Pattern

<action path="/shop/viewOrder" type="com.ibatis.struts.BeanAction"
name="orderBean" scope="session"
validate="false">
<forward name="success" path="/order/ViewOrder.jsp"/>
</action>


此种方式表示,控制将被转发到"orderBean"这个form bean对象 的"viewOrder"方法(行为)来处理。方法名取"path"参数的以"/"分隔的最后一部分。
2. Method Parameter <action path="/shop/viewOrder" type="com.ibatis.struts.BeanAction"
name="orderBean" parameter="viewOrder" scope="session"
validate="false">
<forward name="success" path="/order/ViewOrder.jsp"/>
</action>


此种方式表示,控制将被转发到"orderBean"这个form bean对象的"viewOrder"方法(行为)来处理。配置中的"parameter"参数表示form bean类上的方法。"parameter"参数优先于"path"参数。
3. No Method call <action path="/shop/viewOrder" type="com.ibatis.struts.BeanAction"
name="orderBean" parameter="*" scope="session"
validate="false">
<forward name="success" path="/order/ViewOrder.jsp"/>
</action>

此种方式表示,form bean上没有任何方法被调用。如果存在"name"属性,则struts把表单参数等数据填充到form bean对象后,把控制转发到"success"。否则,如果name为空,则直接转发控制到"success"。
这就相当于struts内置的org.apache.struts.actions.ForwardAction的功能 <action path="/shop/viewOrder" type="org.apache.struts.actions.ForwardAction"
parameter="/order/ViewOrder.jsp " scope="session" validate="false">
</action>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值