posts - 188,comments - 176,trackbacks - 0

对于struts框架,我们都知道我们写的*Action类是继承struts的Action(org.apache.struts.action.Action)类,并重写其定义的execute方法,进而来实现我们自己的业务逻辑。

但考虑到到一些需求,我们可以在struts的Action和我们自己写的*Action类之间加一层Action类来实现过滤功能,我们将其定义为BaseAction,整个继承关系:*Action--extends---->BaseAction---extends--->Action。

举例如下:

在ListAction类和Struts框架的Action类之间加一层BaseAction类。

ListAction:

//extends BaseAction
public class ListAction extends BaseAction {

 public ActionForward doExecute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception {
  
  Connection conn = null;
  conn = this.getDataSource(request).getConnection();
  BookDAO bookDAO  = DAOFactory.getBookDAO(conn);
  List list = bookDAO.findAll();
  request.setAttribute("books", list);
  return mapping.findForward(Constants.FORWARD_LIST); 
 }
 
 //实现BaseAction中的needLogin方法,判断*Action是否需要登陆验证
 public boolean needLogin() {  
  return true;
 }
}

BaseAction:

public abstract class BaseAction extends Action {

  //override the method execute of Action  
 public final ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {

  if(needLogin()){
     //取出LoginAction中放入session中的用户名"aaa"
     Object o = request.getSession().getAttribute("aaa");
   if(o == null){
     //登陆验证失败,返回login.jsp
     return actionMapping.findForward("login");
   }
  } 
  //返回调用ListAction中的doExcute()方法,执行业务逻辑
  return doExecute(actionMapping, actionForm, request, response);
 }

 //abstract method
 public abstract ActionForward doExecute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest  request, HttpServletResponse response)  throws Exception;
 
 //abstract method
 public abstract boolean needLogin();

}

在BaseAction中重写struts中Action的excute()方法,在ListAction中定义doexcute()方法并extends BaseAction类,登陆系统是根据login.do进入LoginAction,调用重写struts中的excute()方法,此时LoginAction没有,就到父类BaseAction去调用excute()方法,执行其业务逻辑后,如过用户是已经登陆过就返回doExcute()方法,回到ListAction调用doExcute()的业务逻辑,如果是非登陆用户则直接转向Login.jsp。这里将java中多态,抽象类以及回调的思想体现得淋漓尽致。


posted on 2007-05-25 10:16 cheng 阅读(4441) 评论(11)  编辑  收藏 所属分类: Struts

FeedBack:
# re: BaseAction的java多态思考
2007-05-25 10:59 | framework
这种方式很好的,但实际应用时要比这个复杂, 要进行方法分派  回复  更多评论
  
# re: BaseAction的java多态思考
2007-05-25 11:03 | cheng
恩,这是一个基本的实现,个人感觉关键是思想意识的形成。
当然具体问题肯定就要具体去分析了~~~
因为还没有接触到大的项目,所以,呵呵。目前还没有体会到你那种意境了~~~
只有等以后做项目当中去体会和提升了~!  回复  更多评论
  
# re: BaseAction的java多态思考
2007-05-25 11:29 | dennis
把权限的判断分散到各个action中并不是个好主意,用filter可以搞定的事情没必要这么复杂  回复  更多评论
  
# re: BaseAction的java多态思考[未登录]
2007-05-25 11:46 | Aqing
用过滤器实现比较简单:

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.IOException;

/**
* 用于检测用户是否登陆的过滤器,如果未登录,则重定向到指的登录页面 配置参数 redirectURL 如果用户未登录,则重定向到指定的页面,URL不包括
* ContextPath notCheckURLList不做检查的URL列表,以分号分开,并且 URL 中不包括 ContextPath
* checkSessionKey 需检查的在 Session 中保存的关键字
*/

public class CheckLoginFilter implements Filter {
protected FilterConfig filterConfig = null;

private String redirectURL = null;

private String sessionKey = null;

private List notCheckURLList = new ArrayList();

public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
redirectURL = filterConfig.getInitParameter("redirectURL");
sessionKey = filterConfig.getInitParameter("checkSessionKey");

String notCheckURLListStr = filterConfig
.getInitParameter("notCheckURLList");

if (notCheckURLListStr != null) {
StringTokenizer st = new StringTokenizer(notCheckURLListStr, ";");
notCheckURLList.clear();
while (st.hasMoreTokens()) {
notCheckURLList.add(st.nextToken());
}
}
}

public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpSession session = request.getSession();

if (sessionKey == null) {
filterChain.doFilter(request, response);
return;
}
if ((!checkRequestURIIntNotFilterList(request))
&& session.getAttribute(sessionKey) == null) {
response.sendRedirect(request.getContextPath() + redirectURL);
return;
}
filterChain.doFilter(servletRequest, servletResponse);
}

private boolean checkRequestURIIntNotFilterList(HttpServletRequest request) {
String uri = request.getServletPath()
+ (request.getPathInfo() == null ? "" : request.getPathInfo());
return notCheckURLList.contains(uri);
}

public void destroy() {
notCheckURLList.clear();
}
}  回复  更多评论
  
# re: BaseAction的java多态思考
2007-05-25 12:10 | cheng
真的,领教了,正在学习中!:P  回复  更多评论
  
# re: BaseAction的java多态思考
2007-05-25 14:32 | 勇敢的永
楼主的思想不错!
学习了,谢谢楼主!  回复  更多评论
  
# re: BaseAction的java多态思考
2007-05-25 16:55 | 千山鸟飞绝
我这里见过基本将struts重写的basicAction。
其实权限管理做得好的化,不需要basicAction。  回复  更多评论
  
# re: BaseAction的java多态思考
2007-05-25 16:58 | 龙卷风驿站
为什么不继承dispathcaction呢

权限为什么不用spring的aop来操作呢  回复  更多评论
  
# re: BaseAction的java多态思考
2007-05-25 19:18 | cheng
关于spring的权限处理~~以前听朋友说到过~
但具体的配置的话还要去查看下资料学习下~~呵呵  回复  更多评论
  
# re: BaseAction的java多态思考[未登录]
2007-05-25 23:20 | bluesky
我更希望无侵入式的框架,不继承  回复  更多评论
  
# re: BaseAction的java多态思考
2009-04-05 17:00 | 仕途得意
good!very  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: