還要請教banq,關於jdonNews中user和group的操作

jacal發表於2006-06-20
在繼續擴充套件jdonNews的時候,我想實現把user新增/移除到group組,我是這樣實現的:
建立DoUserForGroupAction

public class DoUserForGroupAction
    extends Action {
  public DoUserForGroupAction() {
  }
  public ActionForward execute(ActionMapping mapping, ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession();
    String whichDo = (String) request.getParameter("action");
    String forward = "success";
    UserGroupForm ugform = (UserGroupForm)form;
    UserManagerLocal userManager = (UserManagerLocal) WebAppUtil.getEJBService(
        "userManager",  request);
    if(whichDo == null || whichDo.equals("")){
       forward = "failure";
    }else if(whichDo.equals("addUser")){
      userManager.addUserToGroup(ugform.getGroupid(),ugform.getUserid());
    }
    else if(whichDo.equals("removeUser")) {
      userManager.removeUserFromGroup(ugform.getGroupid(),ugform.getUserid());    
    }
    
    System.out.println("Group:"+ugform.getGroupid()+"  Userid"+ugform.getUserid());
    return mapping.findForward(forward);
  }
}

並在userManager(仿照newsManager)中建立兩個方法:addUserToGroup(String groupid,String userid)和removeUserFromGroup(String groupid,String userid)
方法實現如下:

  public void removeUserFromGroup(String groupid, String userid) {
    try{
      jdbcDao.removeUserFromGroup(groupid,userid);
      jdbcDao.clearAllCache();
    }catch(Exception ex){
      logger.error(ex);
    }
  }

  public void addUserToGroup(String groupid, String userid) {
    try{
      jdbcDao.addUserToGroup(groupid,userid);
      jdbcDao.clearAllCache();
    }catch(Exception ex){
      logger.error(ex);
    }
  }

jdbcDao中實現兩個方法:

public void removeUserFromGroup(String groupid,String userid) throws Exception{
    Connection c = null;
    PreparedStatement ps = null;
    try {
      String REMOVE_USER =
          "delete from  T_USER_GROUP where GROUPID=? AND USERID = ?";
      c = dataSource.getConnection();
      ps = c.prepareStatement(REMOVE_USER);
      ps.setString(1, groupid);
      ps.setString(2,userid);
      ps.executeUpdate();
    }
    catch (SQLException se) {
      throw new Exception("SQLException: " + se.getMessage());
    }
    finally {
      if (ps != null) {      ps.close();      }
      if (c != null) {        c.close();       }
    }
  }
  public void addUserToGroup(String groupid,String userid) throws Exception{
      Connection c = null;
      PreparedStatement ps = null;
      try {
        String ADD_USER =
            "insert into T_USER_GROUP values(?,?)";
        c = dataSource.getConnection();
        ps = c.prepareStatement(ADD_USER);
        ps.setString(1,userid);
        ps.setString(2,groupid);
        ps.executeUpdate();
      }
      catch (SQLException se) {
        throw new Exception("SQLException: " + se.getMessage());
      }
      finally {
        if (ps != null) {      ps.close();      }
        if (c != null) {        c.close();       }
      }
  }

JSP頁面中先利用ModeListAction查詢獲得某個group以及該組的userList,新增完組使用者以後重新獲得該組的userList後發現並沒有變化,而資料庫表T_USER_GROUP(USERID,GROUPID)中是成功新增了一條記錄,但重新顯示的時候並沒有,應該是快取的問題,但我在userManager中的addUserToGroup方法中已經呼叫jdbcDao.clearAllCache()來清除快取了,怎麼還沒有清楚成功?這點比較困惑。
另外,我採用EJB服務的方式實現了user和group的增、刪、改、查,都是採用jdonframework的配置實現的,如果要實現user加入組這樣的功能,採用我上面的方式是否合適?有沒有更加合理的方法?

相關文章