还要请教banq,关于jdonNews中user和group的操作

06-06-20 jacal
在继续扩展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加入组这样的功能,采用我上面的方式是否合适?有没有更加合理的方法?

banq
2006-06-21 14:55
> jdbcDao.clearAllCache();

还是老问题,关键是PageIterator必须是同一个实例,因为缓存被内置在PageIterator内部,在EJB中很难保证操作到是同一个实例,这是一个比较头疼的问题,我也没有想好。这也是EJB独立容器带来的问题。在EJB下干脆失效PageIterator缓存,这其实影响不大,只是失效ID集合缓存。

在非EJB情况下,由于通过Ioc容器(相比独立容器,我称为寄生容器)注射的都是单例,因此,比较容易作答PageIterator是同一个实例。

jacal
2006-06-21 15:10
感谢banq的回复

再ejb中如何让PagaItartor失效呢?需要配置什么?

banq
2006-06-21 15:37
调用PageIteratorSolver的setCacheEnable设置为false即可

jacal
2006-06-22 12:46
请教banq,我建立了一个model:news.model.Group

package news.model;

import com.jdon.controller.model.Model;
import java.util.Collection;
import java.util.ArrayList;

public class Group extends Model{
  public Group() {
  }
  private String groupid;
  private String groupname;
  private Collection groupUsers= new ArrayList();
  public Collection getGroupUsers(){
    return this.groupUsers;
  }
  public void setGroupUsers(Collection groupUsers){
    this.groupUsers = groupUsers;
  }
  public void setGroupid(String groupid){
    this.groupid = groupid;
  }
  public String getGroupid(){
    return this.groupid;
  }
  public void setGroupname(String groupname){
    this.groupname = groupname;
  }
  public String getGroupname(){
    return this.groupname;
  }
}
<p>

下面的getGroup方法想获得一个group,并将这个group的所有用户查询出来放到group model的groupUsers集合中,但调试的时候发现,每个组下虽然有多个用户,但都只能取得一个,即 System.out.println(

"getGroup()-------------------------: " + i);输出的都是:

12:45:47,421 INFO [STDOUT] getGroup()-------------------: 1

12:46:30,281 INFO [STDOUT] getGroup()-------------------: 1

12:46:30,390 INFO [STDOUT] getGroup()-------------------: 1

12:46:30,484 INFO [STDOUT] getGroup()-------------------: 1

下面是getGroup的方法代码:

 public Group getGroup(String groupid) throws Exception {
     //  logger.debug(" getUserByUserID " + groupid);
       Group ret = null;
       try {
         //获取组信息
         String GET_GROUP =
             "select  * from T_GROUP where GROUPID = ?";
         List queryParams = new ArrayList();
         queryParams.add(groupid);

         List list = pageIteratorSolverOfGroup.queryMultiObject(queryParams,
             GET_GROUP);
         Iterator iter = list.iterator();
         if (iter.hasNext()) {
           Map map = (Map) iter.next();
           ret = new Group();
           ret.setGroupid(groupid);
           ret.setGroupname( (String) map.get("GROUPNAME"));

           //查询该组的所有User信息
           List queryParams2 = new ArrayList();
           queryParams2.add(ret.getGroupid());
           String GET_GROUP_USERS =
               "select  T_USER.* from T_USER,T_USER_GROUP where T_USER.USERID=T_USER_GROUP.USERID AND T_USER_GROUP.GROUPID=?";
           pageIteratorSolverOfUser.clearCache();
           List list2 = pageIteratorSolverOfUser.queryMultiObject(queryParams2,
               GET_GROUP_USERS);
           Iterator iter2 = list2.iterator();

           Collection groupUsers = new ArrayList();
           int i = 0;
           if (iter2.hasNext()) {
             Map map2 = (Map) iter2.next();
             User user = new User();
             i = i + 1;
             System.out.println(
                 "getGroup()-------------------------: " + i);
             user.setUserid( (String) map2.get("USERID"));
             user.setUsername( (String) map2.get("USERNAME"));
             user.setPassword( (String) map2.get("PASSWORD"));
             user.setRealname( (String) map2.get("REALNAME"));
             user.setEmail( (String) map2.get("EMAIL"));
             user.setTelephone( (String) map2.get("TELEPHONE"));
    
             groupUsers.add(user);
           }
           ret.setGroupUsers(groupUsers);
         }
       }
       catch (Exception se) {
         throw new Exception("getGroup() SQLException: " + se.getMessage());
       }
       return ret;

// 在此输入java代码
<p>

jacal
2006-06-22 15:16
不好意思,问题解决了,原来if (iter2.hasNext()) 要改成while(iter2.hasNext())

太疏忽了

猜你喜欢