Search code examples
jspjakarta-eestrutsdisplaytag

Last row repeats in DisplayTag table


I get all rows in displaytag table same values i.e. It displays same values for all rows (i.e. last row values)

Below is the Action class snippet where I set the values in formbean

public class FormIdSearch extends Action {

    ResourceBundle rsBundle = ResourceBundle.getBundle("com.mindcraft.resources.ApplicationResource");
    Collection formbeanCol = new ArrayList();

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Collection a = new ArrayList();
        FormBean formbean = (FormBean)form;
        ActionForward forward = new ActionForward();
        ActionMessages message = new ActionMessages();
        CMBConnection connection=new CMBConnection();
        HRMSUtill hrms = new HRMSUtill();
        IngUtill ing = new IngUtill();
        AppUtill au = new AppUtill();
        try{


            String polNo=formbean.getPolicyNo();
            String appN=(String)request.getAttribute("appno");
            System.out.println("Application No.1" + appN);
            String applnNo=formbean.getAppno();
            System.out.println("Application No.2" + applnNo);
            String appNo=(String)request.getParameter("appno");
            System.out.println("Application No.3" + appNo);
            HashMap<String, String> inpMap = new HashMap<String, String>();

            CMBSearchResults srchResults = null;
            CMBItem item = null;

            inpMap.put(rsBundle.getString("CM_INDEXCLASS_ADVISORSLICENSING"), rsBundle.getString("CM_INDEXCLASS_ADVISORSLICENSING"));
            //inpMap.put(rsBundle.getString("CM_INDEXCLASS_ADVISORSLICENSING"), rsBundle.getString("CM_INDEXCLASS_ADVISORSLICENSING"));
            inpMap.put(rsBundle.getString("CM_COMMON_ADVISORCODE"), "AA5555");
            inpMap.put(rsBundle.getString("CM_COLNAME_ADVISORCODE"), rsBundle.getString("CM_COLNAME_ADVISORCODE"));
            inpMap.put(rsBundle.getString("CM_COMMON_APPNUMBER"), appNo);
            inpMap.put(rsBundle.getString("CM_COLNAME_APPNUMBER"), rsBundle.getString("CM_COLNAME_APPNUMBER"));

            String winId=(String)request.getSession().getAttribute("windowsId");
            String empId = au.getEmpCodeOnWinId(winId);

            System.out.println("winId===="+winId);
            String ingBrCode="";

            if(hrmsBrCode1.equalsIgnoreCase(ingBrCode1))
            {

                ServletContext servletcontext = request.getSession().getServletContext();
                String cmbicmsrvsPath = servletcontext.getRealPath("WEB-INF/cmbicmsrvs.ini");
                System.out.println("cmbicmsrvsPath "+cmbicmsrvsPath);

                String icmsrvfilePath=servletcontext.getRealPath("WEB-INF/cmbicmsrvs.ini");
                String icmmenvfilePath=servletcontext.getRealPath("WEB-INF/cmbicmenv.ini");

                connection = new CMBConnection();
                //CMConnectionPool cmConnect = new CMConnectionPool();
                CMSearchDocuments searchDoc = new CMSearchDocuments();


                CMConnectionPool.setIcmenvfile(icmmenvfilePath);
                CMConnectionPool.setIcmsrvfile(icmsrvfilePath);
                //System.out.println("HHHEYEEEYYEYE TRUE.........3");
                connection=CMConnectionPool.getConnection();


                srchResults=searchDoc.searchForDocumentsInCM(connection, inpMap);

                Integer searchCount=srchResults.getCount();

                System.out.println("searchCount "+searchCount);
                LoggerUtil.logger.debug("searchCount "+searchCount);
                HttpSession session = request.getSession(false);
                session.setAttribute("searchCount",searchCount.toString());
                for (int i = 0; i < searchCount; i++) 
                   {
                    item = srchResults.getItem(i);

                    a=getDocumentMetaData(item,formbean);
                      String pid=item.getPidString();
                      request.setAttribute("pid", pid);
                      formbean.setPid(pid);

                   }
                request.setAttribute("formbeanCol", a);
                Object ia[] = a.toArray(); 
                int sum = 0; 
                // sum the array 
                for(int i=0; i<ia.length; i++) {
                sum += ((Integer) ia[i]).intValue(); 
                System.out.println("Sum is: " + sum); 
                } 

                if(searchCount>0){
                    //for (int i = 0; i < srchResults.getCount(); i++){
                    item = srchResults.getItem(searchCount-1);
                    //getDocumentMetaData(item);

                    String pid = item.getPidString();
                    System.out.println("pid "+pid);
                    request.setAttribute("pid", pid);

                    forward=mapping.findForward("success");

                }
                else
                {   
                    forward=mapping.findForward("failure");
                    message.add("ErrorOccurred", new ActionMessage("Invalid.SearchResult"));
                }
                System.out.println("Search Done");
                LoggerUtil.logger.debug("Search Done");
            }
            else
            {

                System.out.println("No Record Found");
                LoggerUtil.logger.debug("No Record Found");

                forward=mapping.findForward("failure");
                message.add("ErrorOccurred", new ActionMessage("Invalid.SearchResult"));
            }
            saveMessages(request, message);
        }
        catch(Exception e){
            forward=mapping.findForward("failure");
            LoggerUtil.logger.debug("Exception in FormIdSearch ",e);
            e.printStackTrace();
        }
        finally{
            try{
                if(connection!=null){
                    CMConnectionPool.freeConnection(connection);
                }
            }
            catch(Exception e)
            {
                forward=mapping.findForward("failure");
                LoggerUtil.logger.debug("Exception in FormIdSearch in finally block Closing CM Connection",e);
            }
        }
        return forward;
    }
     public Collection getDocumentMetaData(CMBItem item,FormBean formbean)
     throws CMBException, Exception 
{

     // Get document metadata attributes
        String FormID = item.getAttrValue(0);
        System.out.println(item.getAttrValue(0));
        System.out.println(item.getAttrValue(1));
        System.out.println(item.getAttrValue(2));
        System.out.println(item.getAttrValue(3));
        System.out.println(item.getAttrValue(4));
        formbean.setFormID(FormID);
        String MainDocument=item.getAttrValue(1);
        formbean.setMainDocument(MainDocument);
        String SubDocument=item.getAttrValue(2);
        formbean.setSubDocument(SubDocument);
        formbean.setAppNumber(item.getAttrValue(3));
        formbean.setBranchCode(item.getAttrValue(4));
        formbean.setAdvisor_NAME(item.getAttrValue(5));
        formbean.setAdvisor_Code(item.getAttrValue(6));


         formbeanCol.add(formbean);
         System.out.println("formbeanCol.size() "+formbeanCol.size());


  return formbeanCol;
}
}  

JSP

<display:table name="formbeanCol" export="false" pagesize="20" sort="list" id="data" requestURI="" class="tablelist">

        <display:column title="advisor_Code" sortable="true" property="advisor_Code"></display:column>
        <display:column title="formID" sortable="true" property="formID"></display:column>
        <display:column title="mainDocument" sortable="true" property="mainDocument"></display:column>
        <display:column title="SubDocument" sortable="true" property="subDocument"></display:column>
        <display:column title="AppNumber" sortable="true" property="appNumber"></display:column>
        <display:column title="branchCode" sortable="true" property="branchCode"></display:column>
        <display:column title="advisor_NAME" sortable="true" property="advisor_NAME"></display:column>

</display:table>

below table I get as an output


Solution

  • The issue is that you are using the single instance of FormBean (the one that's passed into the Action by Struts) in getDocumentMetaData. So your collection (formbeanCol) ends up with each entry pointing to the same FormBean instance. So in your last iteration you will be setting the values for this FormBean to the last data row, and all entries point to this.

    I would advise not using FormBean for this (use a simple bean instead), and create a new instance each time in getDocumentMetaData. If you do want to continue to use FormBean then a simple fix is to change getDocumentMetaData as follows:

    public Collection getDocumentMetaData(CMBItem item,FormBean formbean)
    throws CMBException, Exception 
    {
        FormBean bean = new FormBean();
    
        // Get document metadata attributes
        String FormID = item.getAttrValue(0);
        bean.setFormID(FormID);
    
        // etc
    
        formbeanCol.add(bean);
        return formbeanCol;
    }
    

    This issue does highlight the dangers of using 'global' variables (in this case formbean and formbeanCol).

    FYI, you should avoid instance variables in Actions as Actions in Struts 1.x are globally scoped, i.e. one instance will be shared between different requests. Your formbeanCol will probably get increasingly large and you may get concurrency issues. Just declare formbeanCol as a local variable in the execute method.