struts action1与webwork2扩展性比较

    struts的可扩展性是基于类继承的,而webwork2的扩展是基于拦截器模式的。两者可以都可以达到相同的目的。但是前着不够灵活,特别是顺序,继承的顺序是固定的。而后者比较灵活,拦截器的顺序是可以自由改变的。
    例如:要实现一个判断用户是否已登陆的功能。用struts,我们需要扩展RequestProcessor这个类。
public   class  AuthenticationRequestProcessor  extends  RequestProcessor ...... {
    
    
private Log log = LogFactory.getLog(getClass());
    
    
public AuthenticationRequestProcessor() ......{
        
super();
    }

    
    
protected ActionForward processActionPerform(HttpServletRequest request, 
                                                                              HttpServletResponse response, 
                                                                              Action action, 
                                                                              ActionForm form, 
                                                                              ActionMapping mapping) 
                                                                              
throws IOException, ServletException ......{        
        User user 
= (User)request.getSession().getAttribute(BaseAction.LoginUser_Key);
        
if(user != null)......{
                         
try ......{
                        
return action.execute(mapping, form, request, response);
                    }
 catch (Exception e) ......{
                        
return (processException(request, response,
                                e, form, mapping));
                    }
        
            }
else return mapping.findForward("logon");
    
    }

相同功能如果要在webwork2中实现,我们需要开发一个拦截器。
public   class  AuthenticationInterceptor  implements  Interceptor  {
public static final String USER = "user";
public void destroy() {
}

public void init() {
}

public String intercept(ActionInvocation actionInvocation)
throws Exception {
Map session 
= actionInvocation.getInvocationContext()
.getSession();
User user 
= (User) session.get(USER);
if (user == null{
return Action.LOGIN;
}
 else {
Action action 
= actionInvocation.getAction();
if (action instanceof UserAware) {
((UserAware)action).setUser(user);
}

return actionInvocation.invoke();
}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值