I'm a newbie in Struts 2 and I want to create an arraylist in JSP using Struts tags. And then, the values inputted should be passed to an action. Also, how do I get it back from action into JSP?
Bean
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;
}
}
Action
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>
The problem is I don't know how can the user be able to input a value in my arraylist. And how can I get it from JSP so I can use it in my action? Thank you in advance.
New 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>
Action
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;
}
}
You're very close in the JSP, but really far in the Action
:
A POJO shouldn't extend ActionSupport
(in the same way as an Action
shouldn't contain a List of actions);
An action method should never return null
, it should instead return a result mapped to a JSP or some other view, eg. return SUCCESS;
The getBookingList();
alone doesn't make any sense;
The System.out.println(getBookingList());
won't print the details (eg. amount
) of each element, search how to do it properly;
You need to target the action attribute, not the object pushed by the iterator with var=""
, hence:
<s:textfield name="bookingList[%{#key.index}].amount"/>
The rest is right; you need to create an <s:textfield />
(or an <s:hidden/>
) for each field you need to send to the target action.