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