Java Struts2 - 如何使用 struts 标签创建数组列表,由用户填充,然后将值传递给操作?
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 中创建一个数组列表。然后,输入的值应该传递给一个动作。另外,如何将它从操作中恢复到 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
中非常远:
动作
POJO 不应扩展 ActionSupport
(与 Action
不应包含操作列表相同);
action 方法不应该 return null
,它应该 return 映射到 JSP 或其他视图的结果,例如. return SUCCESS;
单独的getBookingList();
没有任何意义;
System.out.println(getBookingList());
不会打印每个元素的详细信息(例如amount
),请搜索如何正确打印;
JSP
您需要以 action 属性为目标,而不是使用 var=""
的迭代器推送的对象,因此:
<s:textfield name="bookingList[%{#key.index}].amount"/>
剩下的就对了;您需要为需要发送到目标操作的每个字段创建一个 <s:textfield />
(或 <s:hidden/>
)。
我是 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;
}
}
您在 JSP 中非常接近,但在 Action
中非常远:
动作
POJO 不应扩展
ActionSupport
(与Action
不应包含操作列表相同);action 方法不应该 return
null
,它应该 return 映射到 JSP 或其他视图的结果,例如.return SUCCESS;
单独的
getBookingList();
没有任何意义;System.out.println(getBookingList());
不会打印每个元素的详细信息(例如amount
),请搜索如何正确打印;
JSP
您需要以 action 属性为目标,而不是使用
var=""
的迭代器推送的对象,因此:<s:textfield name="bookingList[%{#key.index}].amount"/>
剩下的就对了;您需要为需要发送到目标操作的每个字段创建一个
<s:textfield />
(或<s:hidden/>
)。