在JSP中迭代List對象,其值來自ViewAction類,它顯示的是正確的。獲取從jsp到動作類的列表對象值
下面是jps代碼。
<s:iterator value="beanList" status="stat">
<tr>
<td>
<input type="checkbox" name="subCheckBox" />
</td>
<td>
<s:textfield name="beanList[%{#stat.index}].rollnumber"
value="%{rollnumber}" theme="simple"/>
</td>
<td>
<s:textfield name="beanList[%{#stat.index}].name"
value="%{name}" theme="simple"/>
</td>
<td>
<s:textfield name="beanList[%{#stat.index}].location"
value="%{location}" theme="simple"/>
</td>
</tr>
</s:iterator>
ViewAction.java和Bean類代碼如下
在動作類列表對象名稱是beanList
public class ViewCheckboxAction extends ActionSupport {
HttpServletRequest request = ServletActionContext.getRequest();
String viewData = "select * from student order by rollno";
List<Bean> beanList;
public List<Bean> getBeanList() {
return beanList;
}
public void setBeanList(ArrayList<Bean> beanList) {
this.beanList = beanList;
}
public String execute() {
beanList = new ArrayList<Bean>();
DbConnection db = new DbConnection();
int counter = 0;
try {
Statement st = db.getConnection().createStatement();
ResultSet res = st.executeQuery(viewData);
while(res.next()) {
counter++;
Bean bean = new Bean(res.getInt(1),
res.getString(2),
res.getString(3));
rollNumber.add(res.getString("rollno"));
beanList.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
db.removeConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(counter>0)
return SUCCESS;
else
return ERROR;
}
}
豆:
public class Bean {
int rollnumber;
String name;
String location;
public Bean(int x, String y, String z) {
rollnumber = x;
name = y;
location = z;
}
getters and setters...
我需要多個/單個更新表單字段值從jsp到動作 類以便執行更新的操作。但是這個列表(beanList) 的值在操作類中是無效的。既然它是無效的,我不能做更新 操作。 1)在新的動作類(EditAction.java)中如何初始化列表對象(beanList)? 這與我在ViewAction.java中聲明的方式相同 2)Jsp sysntax是否正確? 請求你幫忙。提前致謝。
非常感謝它工作正常:)爲什麼Bean類中不需要參數構造函數?請幫助我。 – Prakash
如果它有效,你應該upvote並接受答案。順便說一句,我現在編輯,讓你知道發生了什麼 –
嘿,很好的捕獲。甚至沒有看到OP中的Bean類。 –