2012-11-02 24 views
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來執行。

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個不同的包含業務邏輯的Class文件。所以不是每個servlet調用Comment.getList()我想我應該有3個評論類:

SupervisorComment.getList(); 
ManagerComment.getList(); 
PresidentComment.getList(); 
+0

構架哪些是你使用?或者沒有關係? – sndyuk

+0

不使用框架。 – jeff

+0

如果我在案例中編寫代碼,我使用了具有自定義註釋的依賴注入。 – sndyuk

回答

1

這是我現在剛創建的示例代碼(我沒有調試...):https://gist.github.com/4019608#comments

+0

+1,用於非常詳細的示例代碼。讓我學習一天左右。 +1努力工作 – jeff

+0

好的遺憾的是,工作並未安裝Java 7,所以我無法訪問java.nio.file。 BeanFactory不適合我。將嘗試重構向Java 6 – jeff

+0

@jeff爲什麼你不嘗試它自己。我不想寫一種古老的語言。我認爲你應該創建另一個關於java7的問題。 – sndyuk