我得到displaytag表中的所有行相同的值,即它显示所有行的相同值(即最后一行值)
下面是Action类片段,我在其中设置了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>
答案 0 :(得分:2)
问题是您正在getDocumentMetaData中使用FormBean的单个实例(Struts传递给Action的那个实例)。因此,您的集合(formbeanCol)最终会指向同一个FormBean实例的每个条目。因此,在上一次迭代中,您将把此FormBean的值设置为最后一个数据行,并且所有条目都指向此。
我建议不要使用FormBean(改为使用简单的bean),每次在getDocumentMetaData中创建一个新实例。如果您确实想继续使用FormBean,那么一个简单的修复方法是更改getDocumentMetaData,如下所示:
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;
}
这个问题突出了使用'全局'变量(在本例中为formbean和formbeanCol)的危险。
仅供参考,你应该避免在Actions中的实例变量,因为Struts 1.x中的Actions是全局范围的,即一个实例将在不同的请求之间共享。您的formbeanCol可能会变得越来越大,您可能会遇到并发问题。只需在执行方法中将formbeanCol声明为局部变量。