1

私は3つのサーブレットを持っています

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

各サーブレットは Struts スタイルの actionMap を使用します。たとえば、各サーブレットが呼び出されると、 ?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
  }

誰がログインしているか、どのサーブレットを呼び出しているかに基づいて、さまざまな 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 ロジックを使用する唯一の方法は? または、各サーブレットで特定の SupervisorCommentClass.getList()、ManagerCommentClass.getList() などを呼び出す必要がありますか?

==EDIT-- 週末にかけて、私はこれについて考えました。3 つの個別のサーブレットと、サーブレットごとに 3 つの個別のビューがあるため、ビジネス ロジックを含む 3 つの個別のクラス ファイルを用意するのが最も理にかなっているようです。したがって、各サーブレット呼び出しの代わりに、Comment.getList()3 つのコメント クラスが必要だと思います。

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

1 に答える 1

1

これは、私が今作成したサンプルコードです (デバッグはしていません...): https://gist.github.com/4019608#comments

于 2012-11-05T19:03:58.960 に答える