簡體   English   中英

Java Struts2-如何使用struts標簽創建要由用戶填充的arraylist,然后將值傳遞給操作?

[英]Java Struts2 - How do I create an arraylist using struts tags, to be populated by user then the values should be passed to action?

我是Struts 2的新手,我想使用Struts標記在JSP中創建一個arraylist。 然后,應將輸入的值傳遞給操作。 另外,如何將其從操作返回到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;
    }

}

您在JSP中非常接近,但在Action卻非常遙遠:

行動

  1. POJO不應該擴展ActionSupport (就像Action不應該包含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. 其余的是對的; 您需要為需要發送到目標操作的每個字段創建一個<s:textfield /> (或一個<s:hidden/> )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM