Bean
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Action
public class LoginAction extends ActionSupport {
private BaseDao baseDao;
private String username;
private String password;
private List<User> list;
public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String Login() {
list=baseDao.Login();
return SUCCESS;
}
public void setBaseDao(BaseDao baseDao) {
this.baseDao = baseDao;
}
}
Dao
public class BaseDao extends JdbcDaoSupport {
// public List Login() {
// // org.logicalcobwebs.proxool.ProxoolDataSource dataSource =
// // (ProxoolDataSource) this
// // .getDataSource();
//
// List list = this.getJdbcTemplate().queryForList("select * from user");
// for (Object obj : list) {
// System.out.println(obj.toString());
// }
// List ls = BaseDao.ChangeList(list);
// this.getJdbcTemplate().batchUpdate(
// "insert into user (username,password) values(?,?)",
// new MyBatchPreparedStatementSetter(ls));
// return null;
// }
// private static List<User> ChangeList(List ls) {
// if (ls == null || ls.size() <= 0) {
// return null;
// }
// List<User> l_user = new ArrayList<User>();
// for (int i = 0; i < ls.size(); i++) {
// Map m = (Map) ls.get(i);
// User user = new User();
// user.setUsername(m.get("username").toString());
// user.setPassword(m.get("password").toString());
// l_user.add(user);
// }
// return l_user;
// }
// private class MyBatchPreparedStatementSetter implements
// BatchPreparedStatementSetter {
// final List temList;
//
// /** 通过构造函数把要插入的数据传递进来处理 */
// public MyBatchPreparedStatementSetter(List list) {
// temList = list;
// }
//
// public int getBatchSize() {
// return temList.size();
// }
//
// public void setValues(PreparedStatement ps, int i) throws SQLException {
// User user = (User) temList.get(i);
// ps.setString(1, i + "" + user.getUsername());
// ps.setString(2, i + "" + user.getPassword());
// }
// }
public List<User> Login() {
List list = this.getJdbcTemplate().queryForList("select * from user");
final List<User> ls = BaseDao.ChangeList(list);
//向数据库批量插入数据
// this.getJdbcTemplate().batchUpdate(
// "insert into user (username,password) values(?,?)",
// new BatchPreparedStatementSetter() {
// public int getBatchSize() {
// return ls.size();
// }
//
// public void setValues(PreparedStatement ps, int i)
// throws SQLException {
// User user = ls.get(i);
// ps.setString(1, i + "" + user.getUsername());
// ps.setString(2, i + "" + user.getPassword());
// }
//
// });
return ls;
}
private static List<User> ChangeList(List ls) {
if (ls == null || ls.size() <= 0) {
return null;
}
List<User> l_user = new ArrayList<User>();
for (int i = 0; i < ls.size(); i++) {
Map m = (Map) ls.get(i);
User user = new User();
user.setUsername(m.get("username").toString());
user.setPassword(m.get("password").toString());
l_user.add(user);
}
return l_user;
}
}
jsp页面
<table border="1" bordercolor="#000000">
<tr>
<td>
序号
</td>
<td colspan="2">
标题1
</td>
<td colspan="2">
标题2
</td>
<td colspan="2">
标题3
</td>
<td colspan="2">
标题4
</td>
</tr>
<s2:iterator value="list" status="status">
<s2:if test="#status.index%4==0||#status.first">
<tr>
<td nowrap="nowrap">
第<s2:property value="(#status.index+1)/4" />行
</td>
</s2:if>
<td>
<s2:property value="username" />
</td>
<td>
<s2:property value="password" />
</td>
<s2:if test="(#status.index+1)%4==0">
</tr>
</s2:if>
<s2:if test="#status.last">
</tr>
</s2:if>
</s2:iterator>
</table>