[旧作]我的技术架构之二-struts1.1中通过Spring对ACTION进行横切

 

由于Struts2.0以下版本设计先天的原因,struts的理念并不先进(这一点比同样基于请求的webwork框架差很多,不过现在struts大大把webwork给吞了很久,不知道消化的如何了).在struts中对ACTION进行横切只能靠其它方法了.

现在我就在这里给出如何对struts action进行横切的一种简单方法:

1,在web.xml中配置spring

2,在struts-config.xml中配置如下信息:
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/action-servlet.xml"/>
</plug-in>

3,在struts-config.xml中配置ACTION代理:

<action
attribute="xxxForm"
name="xxxForm"
scope="request"
path="/xxxAction"
parameter="method"
type="org.springframework.web.struts.DelegatingActionProxy"
validate="false">
.........
</action>

4,在action-servlet.xml做如下配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="autodetect">
<!-- 你的业务ACTION-->
<bean name="/xxxAction" class="xxx.yyy.zzz.YourAction"/>
<!-- 你的横切面-->
<bean id="secureAdvice" class="xxx.yyy.YourAdvice"/>
<!--autoProxyCreator -->
<bean name="secureAutoProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>/xxxAction</value>
</list>
</property>
<property name="interceptorNames">
<list>
<idref local="secureAdvice"/>
</list>
</property>
<property name="optimize" value="true"/>
</bean>
</beans>

5,当然,要先写好一个很普通的struts Action 和 ActionForm,但是也别忘了,还有一个横切面类:YourAdivice.这里简单的介绍一下YourAdivice:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ajaxanywhere.AAUtils;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class SecureAroundAdvice implements MethodInterceptor {
private static Log _log=LogFactory.getLog(SecureAroundAdvice.class);
public Object invoke(MethodInvocation invocation) throws Throwable {
Object[] args=invocation.getArguments();
ActionMapping mapping=(ActionMapping)args[0];
ActionForm form=(ActionForm)args[1];
HttpServletRequest request=(HttpServletRequest)args[2];
HttpServletResponse response=(HttpServletResponse)args[3];
ActionForward forward=execute(mapping,form,request,response);
return forward==null?invocation.proceed():forward;
}
//下面这个方法是我为了使得invoke方法简洁,并且迎合struts而写的.不是必须.
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response ) throws Exception{
//这里可以写你的ACTION的横切处理
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值