在 JSP 中迭代 List 对象,其值来自正确显示的 ViewAction 类。
下面是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 到操作类的多个/单个更新的表单字段值才能执行更新操作。但是 list(beanList) 值在操作类中无效。由于它已无效,我无法进行更新操作。1)在新的动作类(EditAction.java)中如何初始化列表对象(beanList)?这与我在 ViewAction.java 中声明的方式相同 2) Jsp sysntax 是否正确?请求您对此提供帮助。提前致谢。