2010-08-04 126 views
2

我在我的Action類中有一個對象,它包含內部對象的數組列表,我試圖爲這個對象創建一個CRUD屏幕。我的Action類和豆類如下,Struts2使用對象的數組列表修改對象

/** 
* @author rohit 
* 
*/ 
public class IRFeedMgmtAction extends ActionSupport implements ModelDriven<IRFeeds>,SessionAware,ServletRequestAware { 

private static org.apache.log4j.Logger log = Logger.getLogger(IRFeedMgmtAction.class); 

private HttpServletRequest request; 

private Map session; 

private IRAccountsDAO acctsDAO; 

private IRFeeds feed = new IRFeeds(); 

/* (non-Javadoc) 
* @see com.opensymphony.xwork2.ActionSupport#execute() 
*/ 
public String execute() 
{ 
    return "success"; 
} 

/** 
* @return 
*/ 
public String add() 
{ 
    IRUser user = (IRUser) session.get("user"); 

    List<IRAccountUsers> twtUsers = acctsDAO.getTwitterAcctByOrgId(user.getOrgId()); 
    feed.setTwtAccts(prepareTwitterAccounts(twtUsers)); 

    return "addFeed"; 
} 

/** 
* @return 
*/ 
public String save() 
{ 
    IRFeeds fd = getFeed(); 
    ArrayList<IRFeedAccts> twtAccts = fd.getTwtAccts(); 

    System.err.println(fd.getFeedUrl()); 

    for (Iterator iterator = twtAccts.iterator(); iterator.hasNext();) 
    { 
     IRFeedAccts irFeedAccts = (IRFeedAccts) iterator.next(); 
     System.err.println(irFeedAccts.getNumber()); 
    } 

    return "saved"; 
} 

/** 
* @return 
* 
*/ 
private ArrayList<IRFeedAccts> prepareTwitterAccounts(List<IRAccountUsers> twtUsers) 
{ 
    ArrayList<IRFeedAccts> twtAccts = new ArrayList<IRFeedAccts>(); 
    IRAccountUsers twtUser = null; 
    IRFeedAccts feedAccnt = null; 
    for (Iterator iterator = twtUsers.iterator(); iterator.hasNext();) 
    { 
     twtUser = (IRAccountUsers) iterator.next(); 

     feedAccnt = new IRFeedAccts(); 
     feedAccnt.setAccountId(twtUser.getSocialId()); 
     feedAccnt.setPic(twtUser.getPic()); 
     feedAccnt.setName(twtUser.getTwtUsrName()); 
     feedAccnt.setNumber(30); 

     twtAccts.add(feedAccnt); 
    } 

    return twtAccts; 
} 

MY BEAN

public class IRFeeds implements java.io.Serializable { 


private Integer feedId; 
private Integer campId; 
private String feedUrl; 
private boolean active; 
private Date createdOn; 
private Date updatedOn; 
private String createdBy; 

private ArrayList<IRFeedAccts> twtAccts; 

private ArrayList<IRFeedAccts> fbAccts; 

private ArrayList<IRFeedAccts> fbPages; 

MY JSP文件

<s:iterator value="#session.fd.twtAccts" status="twtAcct"> 
      <tr> 
       <td> 
        <div style="width: 48px; float: left;"><img src="<s:property value="pic" />" /></div> 
        <div style="text-align: left;"><s:property value="name" /></div> 
       </td> 
       <td> 
        <s:textfield name="number"/> 
       </td> 
       <td> 
        <input type="text" /> 
       </td> 
       <td> 
        <s:textfield name="signature"/> 
       </td> 
      </tr> 
      </s:iterator> 

現在我的問題是,當豆的ArrayList中的值被修改在JSP中,同樣沒有達到動作類保存方法。值保持不變。當您提交豆會去填充表單

問候, 羅希特

回答

1

解決這個問題

<s:iterator id="twtFeedAccts" value="twtFeedAccts" status="twtAcct"> 
      <tr> 
       <td width="250"> 
        <img src="<s:property value="%{twtFeedAccts[#twtAcct.index].pic}" />" width="25px" height="25px" /> 
         <s:property value="%{twtFeedAccts[#twtAcct.index].name}" /> 

       </td> 
       <td width="200"> 
        <s:textfield id="twtFeedAccts[%{#twtAcct.index}].number" name="twtFeedAccts[%{#twtAcct.index}].number" value="%{twtFeedAccts[#twtAcct.index].number}" /> 
       </td> 
       <td width="200"> 
        <s:select id="twtFeedAccts[%{#twtAcct.index}].cycle" name="twtFeedAccts[%{#twtAcct.index}].cycle" value="%{twtFeedAccts[#twtAcct.index].cycle}" 
        label="Select a month" list="#{'2':'2 hrs','4':'4 hrs', '6':'6 hrs', '12':'12 hrs', '24':'24 hrs'}" /> 
       </td> 
       <td width="250"> 
        <s:textfield id="twtFeedAccts[%{#twtAcct.index}].signature" name="twtFeedAccts[%{#twtAcct.index}].signature" value="%{twtFeedAccts[#twtAcct.index].signature}" size="40"/> 
       </td> 
       <td width="50"> 
        <s:checkbox id="twtFeedAccts[%{#twtAcct.index}].selected" name="twtFeedAccts[%{#twtAcct.index}].selected" value="%{twtFeedAccts[#twtAcct.index].selected}" /> 
       </td> 
      </tr> 
      </s:iterator> 

問候, 羅希特夏爾

+0

如果什麼豆已經得到了本身的列表,然後只遍歷的列表是可能的,但是如何在表單提交中將該列表保存在b​​ean中? – 2011-02-12 15:11:26

0

Here是一個工作例子(6.9 Netbeans的項目)示出如何遍歷對象的數組或列表。

此外,如何提交表單,以便在提交時重新創建對象列表。

只需解決參考並開始。

0

我已經解決了這個問題:

我的JSP:

<s:iterator value="feedbackFor" var="feedbackFor" status="stat"> 
<tr> 
<td><s:label> <s:property value="feedbackFor" /></s:label> </td> 


<td><s:label><s:property value="empName"/></s:label></td> 
<s:hidden name="feedbackFor[%{#stat.index}].feedbackBy" value="%{feedbackBy}"></s:hidden> 
<s:hidden name="feedbackFor[%{#stat.index}].feedbackFor" value="%{feedbackFor}"></s:hidden> 
<!--<s:hidden name="feedbackFor.[%{#stat.index}].positiveFeedback" value="%{positiveFeedback}"></s:hidden>--> 
<td><s:textfield name="feedbackFor[%{#stat.index}].positiveFeedback" value="%{positiveFeedback}" size="100"/></td> 
<td><s:textfield name="feedbackFor[%{#stat.index}].negetiveFeedback" value="%{negetiveFeedback}" size="100"/></td> 
</tr> 
</s:iterator> 


</table> 
<s:submit value="Submit Feedback"/> 
</s:form> 

我的行動

public class FeedbackAction extends ActionSupport { 

private static final long serialVersionUID = 1L; 
private List<FeedbackDTO> feedbackFor; 

private String employeeId; 

public List<FeedbackDTO> getFeedbackFor() { 
    return feedbackFor; 
} 

public void setFeedbackFor(List<FeedbackDTO> feedbackFor) { 
    this.feedbackFor = feedbackFor; 
} 


public String getEmployeeId() { 
    return employeeId; 
} 

public void setEmployeeId(String employeeId) { 
    this.employeeId = employeeId; 
} 



public String getEmployees(){ 

    FeedbackHelper feedbackHelper = new FeedbackHelper(); 
    feedbackFor=feedbackHelper.getEmployeeList(employeeId); 
    System.out.println("The feed back populated is "+ feedbackFor + "Size is "+ feedbackFor.size()); 
      return SUCCESS; 
} 

public String submitFeedback(){ 
    System.out.println("The feed back repopulated is "+ feedbackFor + "Size is "+ feedbackFor.size()); 
    return SUCCESS; 
} 

}