Struts2 使用对象数组列表修改对象

发布于 2024-09-12 18:07:13 字数 3263 浏览 4 评论 0原文

我的 Action 类中有一个对象,其中包含内部对象的数组列表,我正在尝试为该对象创建一个 CRUD 屏幕。我的操作类和bean如下所示,

/**
 * @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;
}

我的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;

我的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>

现在我的问题是当在JSP中修改arraylist中的bean的值时,同样不会到达操作类的保存方法。该值保持不变。

问候, 罗希特

I have an object in my Action class which contains an arraylist of objects internally, I am trying to create a CRUD screen for this object. My Action Class and bean are given below,

/**
 * @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 FILE

<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>

Now my problem is when the value of the beans in the arraylist is modified in the JSP, the same doesn't reach the action class save method. The value remains the same.

Regards,
Rohit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

破晓 2024-09-19 18:07:13

解决此问题

<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>

当您提交表单时,bean 将被填充。

问候,
罗希特

Solve this issue

<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>

When you submit the form the beans will go populated.

Regards,
Rohit

天冷不及心凉 2024-09-19 18:07:13

这里是一个工作示例(Netbeans 6.9项目),说明如何迭代对象的数组或列表。

另外,如何提交表单以便在提交时重新创建对象列表。

只需解决参考文献即可开始。

Here is a working example(Netbeans 6.9 project) illustrating how to iterate over an array or list of objects.

Also, how to submit the form such that the list of objects gets re-created on submission.

Simply resolve the references and get going.

只有一腔孤勇 2024-09-19 18:07:13

我已经解决了这个问题:

我的 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;
}

}

I have solved the issue:

My 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>

My Action

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;
}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文