struts2 iterator遍历不出list列表
index.jsp<s:iteratorvalue="userList"var="user"><tr><td><s:propertyvalue="#user.id"/><...
index.jsp
<s:iterator value="userList" var="user">
<tr>
<td><s:property value="#user.id" /></td>
<td><s:property value="#user.name" /></td>
<td><a href="User-update?id=<s:property value="user.id"/>">修改</a>
<a href="User-delete?id=<s:property value="user.id"/>">删除</a></td>
</tr>
</s:iterator>
UserRepository.java
public List<UserVO> list() throws SQLException {
String sql = "select * from tbl_user";
List<UserVO> userList = new ArrayList<UserVO>();
try {
Connection conn = ConnManager.getConnection();
PreparedStatement stat = conn.prepareStatement(sql);
ResultSet rs = stat.executeQuery();
UserVO a = null;
while (rs.next()) {
a = new UserVO();
a.setId(rs.getLong("id"));
a.setName(rs.getString("name"));
userList.add(a);
}
rs.close();
stat.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return userList;
}
UserAction.java
public class UserAction {
private static final long serialVersionUID = 1L;
private List<UserVO> userList;
private UserVO user;
private UserRepository userRepository = new UserRepository();
public String add() {
return "userAdd";
}
public String addPost() {
userRepository.add(getUser());
return "userList";
}
public String userList() {
userList = userRepository.list();
return "userList";
}
userList get和set方法都写了
到底有什么问题呢 我数据库里已经添加进去了 但是页面无法遍历出来 展开
<s:iterator value="userList" var="user">
<tr>
<td><s:property value="#user.id" /></td>
<td><s:property value="#user.name" /></td>
<td><a href="User-update?id=<s:property value="user.id"/>">修改</a>
<a href="User-delete?id=<s:property value="user.id"/>">删除</a></td>
</tr>
</s:iterator>
UserRepository.java
public List<UserVO> list() throws SQLException {
String sql = "select * from tbl_user";
List<UserVO> userList = new ArrayList<UserVO>();
try {
Connection conn = ConnManager.getConnection();
PreparedStatement stat = conn.prepareStatement(sql);
ResultSet rs = stat.executeQuery();
UserVO a = null;
while (rs.next()) {
a = new UserVO();
a.setId(rs.getLong("id"));
a.setName(rs.getString("name"));
userList.add(a);
}
rs.close();
stat.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return userList;
}
UserAction.java
public class UserAction {
private static final long serialVersionUID = 1L;
private List<UserVO> userList;
private UserVO user;
private UserRepository userRepository = new UserRepository();
public String add() {
return "userAdd";
}
public String addPost() {
userRepository.add(getUser());
return "userList";
}
public String userList() {
userList = userRepository.list();
return "userList";
}
userList get和set方法都写了
到底有什么问题呢 我数据库里已经添加进去了 但是页面无法遍历出来 展开
2013-09-05 · 知道合伙人互联网行家
关注
展开全部
首先确认 userRepository.list()返回的userLst是否正确
然后:jsp中增加<s:debug/>标签,看以下valuestake里面 userList是否正确
然后:jsp中增加<s:debug/>标签,看以下valuestake里面 userList是否正确
更多追问追答
追问
debug我有加 显示userList为null
追答
userList = userRepository.list();
这后面加个日志,把userList输出出来看看,如果这里userList有的话,那就是你getter和setter方法的问题了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询