【问题标题】:Java Struts2 - How do I create an arraylist using struts tags, to be populated by user then the values should be passed to action?Java Struts2 - 如何使用 struts 标签创建一个数组列表,由用户填充,然后将值传递给操作?
【发布时间】:2016-04-08 19:34:44
【问题描述】:

我是 Struts 2 的新手,我想使用 Struts 标签在 JSP 中创建一个数组列表。然后,输入的值应该传递给一个动作。另外,如何将其从操作中恢复到 JSP 中?

豆子

public class BookingListAction extends ActionSupport {

    private Boolean isDebit;
    private BigDecimal amount;
    private BigDecimal phpAmount;
    private String currency;
    private String glAccountName;
    private BigDecimal glAccountNumber;
    private String misCode;
    private String branchCode;
    private String branchName;

    public Boolean getIsDebit() {
        return isDebit;
    }

    public void setIsDebit(Boolean isDebit) {
        this.isDebit = isDebit;
    }

    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public BigDecimal getPhpAmount() {
        return phpAmount;
    }

    public void setPhpAmount(BigDecimal phpAmount) {
        this.phpAmount = phpAmount;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public String getGlAccountName() {
        return glAccountName;
    }

    public void setGlAccountName(String glAccountName) {
        this.glAccountName = glAccountName;
    }

    public BigDecimal getGlAccountNumber() {
        return glAccountNumber;
    }

    public void setGlAccountNumber(BigDecimal glAccountNumber) {
        this.glAccountNumber = glAccountNumber;
    }

    public String getMisCode() {
        return misCode;
    }

    public void setMisCode(String misCode) {
        this.misCode = misCode;
    }

    public String getBranchCode() {
        return branchCode;
    }

    public void setBranchCode(String branchCode) {
        this.branchCode = branchCode;
    }

    public String getBranchName() {
        return branchName;
    }

    public void setBranchName(String branchName) {
        this.branchName = branchName;
    }
}

动作

public class BookingAction extends ActionSupport {

    private List<BookingListAction> bookingList = new ArrayList<BookingListAction>();

    public String execute() {

        try {
            getBookingList();
            System.out.println(getBookingList());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public List<BookingListAction> getBookingList() {
        return bookingList;
    }

    public void setBookingList(List<BookingListAction> bookingList) {
        this.bookingList = bookingList;
    }

}

JSP

<%@ taglib prefix="s" uri="/struts-tags" %>

<form id="bookingDebitCreditForm" action="BookingDebitCredit" method="POST">

    <s:textfield name="bookingListItem['0'].amount"/>

    <s:iterator value="bookingList" var="bookingListItem" status="key">
        <s:textfield name="bookingListItem[%{#key.index}].amount"/>
    </s:iterator>

    <button type="button" class="btn btn-flat btn-info" id="saveBookingJson">save</button>
    <button type="button" class="btn btn-info btn-raised">back</button> 

</form>

问题是我不知道用户如何能够在我的数组列表中输入一个值。以及如何从 JSP 中获取它以便在我的操作中使用它?提前谢谢你。

新的 JSP

    <%@ taglib prefix="s" uri="/struts-tags" %>

<form id="bookingDebitCreditForm" action="BookingDebitCredit" method="POST">
    <div>
        <table>
            <tr>
                <td>
                    Amount: 
                </td>
                <td>
                    <s:textfield name="bookingList[%{#key.index}].amount" theme="simple"/>
                </td>
            </tr>
        </table>
            <table> 
            <s:iterator value="bookingDebitCreditList" id="array" status="key">
              <tr>
                  <td><s:textfield name="array[%{#key.index}].amount" value="%{amount}" theme="simple"/></td>
              </tr>
            </s:iterator>
            </table>
    </div>

    <button type="submit" class="btn btn-flat btn-info">save</button>
    <button type="button" class="btn btn-info btn-raised">back</button> 

</form>

动作

    package pnb.cdo.booking.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

import pnb.cdo.booking.service.BookingService;
import pnb.cdo.model.BookingBean;
import pnb.cdo.model.BookingDebitCreditBean;

/**
 * @author MSICX420
 */
@SuppressWarnings("serial")
public class BookingAction extends ActionSupport {

    private List<BookingListBean> bookingList = new ArrayList<BookingListBean>();
    private List<BookingDebitCreditBean> bookingDebitCreditList = new ArrayList<BookingDebitCreditBean>();

    public String execute() {

        BookingBean bookingBean = new BookingBean();
        BookingDebitCreditBean bookingDebitCreditBean = new BookingDebitCreditBean();

        Integer ctr = 0;
        for (BookingListBean item : bookingList) {
            bookingDebitCreditBean.setAmount(bookingList.get(ctr).getAmount());
            bookingDebitCreditBean.setBookingBean(bookingBean);
        }

        bookingDebitCreditList = BookingService.getAllBookingDebitCredit();
        for (BookingDebitCreditBean item : bookingDebitCreditList) {
            System.out.println(item.getAmount());
        }

        if (BookingService.isBookingDebitCreditAdded(bookingDebitCreditBean)) {
            return SUCCESS;
        }

        return NONE;
    }

    /**
     * Getters and setters
     */
    public List<BookingListBean> getBookingList() {
        return bookingList;
    }

    public void setBookingList(List<BookingListBean> bookingList) {
        this.bookingList = bookingList;
    }

    public List<BookingDebitCreditBean> getBookingDebitCreditList() {
        return bookingDebitCreditList;
    }

    public void setBookingDebitCreditList(List<BookingDebitCreditBean> bookingDebitCreditList) {
        this.bookingDebitCreditList = bookingDebitCreditList;
    }

}

【问题讨论】:

    标签: java jsp arraylist struts2 struts-action


    【解决方案1】:

    您在 JSP 中非常接近,但在 Action 中却非常遥远:

    动作

    1. POJO 不应扩展 ActionSupport(就像 Action 不应包含操作列表一样);

    2. 一个动作方法不应该返回null,它应该返回一个映射到JSP或其他视图的结果,例如。 return SUCCESS;

    3. getBookingList(); 本身没有任何意义;

    4. System.out.println(getBookingList()); 不会打印每个元素的详细信息(例如amount),请搜索如何正确执行;

    JSP

    1. 你需要定位action属性,而不是var=""迭代器推送的对象,因此:

       <s:textfield name="bookingList[%{#key.index}].amount"/>
      
    2. 其余的都是对的;您需要为需要发送到目标操作的每个字段创建一个&lt;s:textfield /&gt;(或&lt;s:hidden/&gt;)。

    【讨论】:

    • 感谢您的 cmets 先生。它帮助很大:) 我遵循了每一步,但似乎我在理解第 5 项时遇到了问题。由于我在我的 JSP 中使用 ArrayList,我如何从 JSP 中获取值以便我可以在我的行动? TIA。 &lt;s:textfield name="bookingList[%{#key.index}].amount"/&gt;
    • 该代码正是这样做的:bookingList 寻址数组列表,[number] 指定哪个元素,以及 .amount 属性。什么不工作? P.S. 如果与源操作不同,您显然也需要在目标操作中使用该数组列表
    • 由于某种原因,当我在调试模式下检查它时,我无法从我的 JSP 中获取值,金额字段为空。我已经在新的 JSP 和操作 btw 之上添加了。谢谢。
    • 第一个 s:textfield 在迭代器之外,因此 key.index 在那里没有意义。另一个是array[...]。什么是数组?您需要预订名单:&lt;s:textfield name="bookingList[%{#key.index}].amount"/&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 2021-06-30
    相关资源
    最近更新 更多