以下是我的pojo,Action和jsp页面。
对于Expense
的单个实例,我得到了适当的输入。
我想使用ModelDriven
从jsp页面获取Expense
的多个实例。
为此,我创建了多个<div class="Expense">...</div>
,但我不知道我应该在Action类中做出哪些更改。
如何实现这一点。
Pojo Class
public class Expense implements java.io.Serializable {
private Long id;
private Client client;
private String param;
private BigDecimal value;
private Date dt;
private Date adddate;
//getter and setter
}
ActionClass
public class ExpenxeAction extends ActionSupport implements ModelDriven<Expense> {
Expense e = new Expense();
ExpenseDAO dao = new ExpenseDAO();
private LoginCheck lc = null;
private List<Expense> expenseList = new ArrayList<Expense>();
public String insertExpense() {
dao.insert(e);
return SUCCESS;
}
@Override
public Expense getModel() {
return e;
}
}
Jsp页面
<div class="Expense">
<label>Expense Type</label>
<input type="text" name="param" id="param"/>
<label>Amount</label>
<input type="text" name="value" class="form-control" id="value"/>
<label>Date</label>
<input type="text" name="dt" class="form-control" id="dt"/>
</div>
答案 0 :(得分:0)
您应该为expenseList
public List<Expense> getExpenseList() {
return expenseList;
}
JSP中的使用索引属性名称
<s:iterator value="expenseList" status="stat">
<div class="Expense">
<label>Expense Type</label>
<s:textfield name="expenseList[%{#stat.index}].param" id="param"/>
<label>Amount</label>
<s:textfield name="expenseList[%{#stat.index}].value" class="form-control" id="value"/>
<label>Date</label>
<s:textfield name="expenseList[%{#stat.index}].dt" class="form-control" id="dt"/>
</div>
</s:iterator>
答案 1 :(得分:0)
要获取操作类中的Expense对象的数组列表,您需要对代码进行以下更改。
首先,如果您愿意,您必须将ModelDriven<Expense>
更改为ModelDriven<List<Expense>>
并删除Expense e = new Expense();
。所以,最后你的动作类应该是这样的。
public class ExpenxeAction extends ActionSupport implements ModelDriven<List<Expense>>, Preparable {
ExpenseDAO dao = new ExpenseDAO();
private LoginCheck lc = null;
private List<Expense> expenseList = new ArrayList<Expense>();
public String insertExpense() {
for (Expense e : expenseList) { //loop to iterate over each elements
dao.insert(e);
}
return SUCCESS;
}
public List<Expense> getExpenseList() {
return expenseList;
}
public void setExpenseList(List<Expense> expenseList) {
this.expenseList = expenseList;
}
@Override
public void prepare() throws Exception {
expenseList = new ArrayList<Expense>();
}
@Override
public List<Expense> getModel() {
return expenseList;
}
}
现在,您必须更改jsp页面。
<div class="Expense">
<label>Expense Type</label>
<input type="text" name="model[0].param" id="param"/>
<label>Amount</label>
<input type="text" name="model[0].value" class="form-control" id="value"/>
<label>Date</label>
<input type="text" name="model[0].dt" class="form-control" id="dt"/>
<label>Expense Type</label>
<input type="text" name="model[1].param" id="param"/>
<label>Amount</label>
<input type="text" name="model[1].value" class="form-control" id="value"/>
<label>Date</label>
<input type="text" name="model[1].dt" class="form-control" id="dt"/>
<label>Expense Type</label>
<input type="text" name="model[2].param" id="param"/>
<label>Amount</label>
<input type="text" name="model[2].value" class="form-control" id="value"/>
<label>Date</label>
<input type="text" name="model[2].dt" class="form-control" id="dt"/>
</div>
您会注意到我们在jsp代码中提到了
model[index].<pojo_member_variable_name>
而不是指定lstExp[index].<pojo_member_variable_name>
这是因为现在我们希望struts2将值直接设置到这些成员变量中。所以我们提到model[index].<pojo__member_variable_name>
,以便struts2自动将值设置到列表中。在上面的代码中,我们创建了3个Expense
对象的列表。如果你需要插入更多的值,那么只需增加索引并设置值,剩下的就由struts2为你完成。