81,094
社区成员
发帖
与我相关
我的任务
分享
String sql="select name,password from user";
return (List<User>) qr.query(sql,new BeanListHandler(User.class));
//User.class
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//得到所有用户
public List<User> getAll(){
return udao.getAll();
}
//得到所有用户
public List<User> getAll(){
try {
QueryRunner qr=new QueryRunner(JdbcUtils.getDataSource());
String sql="select * from user";
return (List<User>) qr.query(sql,new BeanListHandler(User.class));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
for (int i = 0; i < list.size(); i++) {
User o = (User) list.get(i);
System.out.println(o.getName()+"test name ====");
}
package cn.itcast.web.controller;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.service.SecurityService;
public class ListUserServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
SecurityService service=new SecurityService();
List list=service.getAll();//获取所有user
request.setAttribute("list",list);
request.getRequestDispatcher("/jsp/listuser.jsp").forward(request,response);
} catch (RuntimeException e) {
throw new RuntimeException(e);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
listuser.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>列出所有用户页面</title>
</head>
<body style="text-align:center;">
<table width="70%">
<tr>
<td align="right">
<a href="${pageContext.request.contextPath}/jsp/adduser.jsp">添加用户</a>
</td>
</tr>
</table>
<table frame="border" width="70%">
<tr>
<td>用户名称</td>
<td>用户密码</td>
<td>操作</td>
</tr>
<c:forEach var="u" items="${list}">
<tr>
<td>${u.name}</td>
<td>${u.password}</td>
<td>
<a href="${pageContext.request.contextPath}/servlet/AddUserRoleUIServlet?user_id=${user.id }">为用户授权</a>
<a href="#">修改用户</a>
<a href="#">删除用户</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>