在 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类代码如下
在action类中列表对象名称是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;
}
}
bean :
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 到操作的多个/单个更新的表单字段值 class 以便执行更新操作。但是列表(beanList) 值在操作类中无效。由于它已失效,我无法进行更新 手术。 1)在新的 Action 类(EditAction.java)中如何初始化列表对象(beanList)? 这与我在 ViewAction.java 中声明的方式相同 2)Jsp sysntax 是否正确? 请求您对此提供帮助。提前致谢。
最佳答案
向您的 Bean
类添加一个默认无参数构造函数。
默认的无参数构造函数被这样调用,因为它是默认的:如果您不指定任何构造函数,它自动创建。
如果您指定另一个构造函数,例如带有与您类似的参数的构造函数,则不再自动创建无参数构造函数,并且您必须显式声明它如果你需要的话。
Struts2 需要无参数构造函数来创建您的 bean。
例如,您可以有一个带有 10 个参数的构造函数的 bean,并在 JSP 页面中仅指定其中一个:Struts 必须能够创建该对象并设置单个字段(通过 Setter),而无需关心九个缺失的参数。
关于java - 从jsp获取List对象值到action类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19551984/