1

我有三个 servlet

/Servelt1 (supervisors, managers & president can execute)
/Servelt2 (only managers & president can execute)
/Servelt3 (only president can execute)

每个 servlet 都使用 Struts 风格的 actionMaps。例如,当调用每个 servlet 时, ?method=list它们都会执行 Comment.getList()

/actionMap.put("list", new ListAction(modelMap, form, "WEB-INF/views/servlet1_v.jsp"));
/actionMap.put("list", new ListAction(modelMap, form, "WEB-INF/views/servlet2_v.jsp"));
/actionMap.put("list", new ListAction(modelMap, form, "WEB-INF/views/servlet3_v.jsp"));


Class Comment {  
//getList called from the 3 servlets
  public List<Comment> getList(HttpServletRequest request) {
     List<Comment> comments = null;
     try {
         CommentDetailDAO cdDao = new CommentDetailDAO();
    comments = cdDao.getComDetailListForDirectReports(authUser.getBadge());
    } catch (DAOException e) {
       setError(FORM_RESULTS, e.getMessage());
    }
    request.setAttribute("comments ", comments);
    return comments; //redundant not really used
  }

那么什么是根据谁登录以及他们正在调用什么 servlet 来执行不同的 Dao 调用。

 if (supervisor) {
   comments = cdDao.getComDetailListForDirectReports(authUser.getBadge());
   //other dao calls 
 }
 if (manager & servlet1) {
   comments = cdDao.getComDetailListForDirectReportsAndTheirDirectReports(authUser.getBadge());
   //other dao calls
 }
 etc...

是使用大量 if/then/else 逻辑的唯一方法吗?或者我应该让每个 servlet 调用特定的 SupervisorCommentClass.getList()、ManagerCommentClass.getList() 等...?

==编辑——周末我想到了这个。由于我有 3 个不同的 Servlet,并且每个 servlet 有 3 个不同的视图,因此拥有 3 个包含业务逻辑的不同类文件似乎是最有意义的。Comment.getList()因此,我认为我应该有 3 个 Comment 类,而不是每个 servlet 调用:

SupervisorComment.getList();
ManagerComment.getList();
PresidentComment.getList();
4

1 回答 1

1

这只是我现在创建的示例代码(我没有调试......):https ://gist.github.com/4019608#comments

于 2012-11-05T19:03:58.960 回答