我是 Struts 2 的新手,我想使用 Struts 标签在 JSP 中创建数组列表。然后,输入的值应该传递给一个 Action 。另外,如何将其从操作返回到 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;
}
}
行动
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
中却很远:
操作
POJO 不应扩展
ActionSupport
(与Action
一样,不应包含操作列表);操作方法永远不应该返回
null
,它应该返回映射到 JSP 或其他 View 的结果,例如。return SUCCESS;
getBookingList();
单独没有任何意义;System.out.println(getBookingList());
不会打印每个元素的详细信息(例如amount
),搜索如何正确执行;
JSP
您需要定位操作属性,而不是迭代器使用
var=""
推送的对象。 ,因此:<s:textfield name="bookingList[%{#key.index}].amount"/>
剩下的都是对的;您需要创建一个
<s:textfield />
(或<s:hidden/>
)对于您需要发送到目标操作的每个字段。
关于Java Struts2 - 如何使用 struts 标签创建数组列表,由用户填充,然后将值传递给操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34603858/