0

这是我的数据库查询功能

        public static List<UserPass> selectAllUser() {
            ConnectionPool pool = ConnectionPool.getInstance();
            Connection connection = pool.getConnection();
            PreparedStatement ps = null;
            ResultSet rs = null;

            List<UserPass> usersPass = new ArrayList<UserPass>();

            String query = "select * from UserPass";
            try{
            ps = connection.prepareStatement(query);
            rs = ps.executeQuery();

            while(rs.next()){
            UserPass users = new UserPass();
            users.setUserName(rs.getString("user_name"));
            usersPass.add(users);

            }

            return usersPass;

这是我的 UserPass 对象获取函数

   public ArrayList<UserPass> getusernames(){

   return usernames;
   }

我的 servlet dopost 就是这样

    request.setAttribute("users", UserPassDb.selectAllUser());

    String forward = "/testpage.jsp";
    RequestDispatcher view = request.getRequestDispatcher(forward);
    view.forward(request, response);

我的显示jsp页面是

            <form action="TestServlet" method="post">
                <input type="submit">

            </form>

            <c:forEach var="user" items="${users}">

                <c:out value="${user.usernames}"/> 

            </c:forEach>

UserPass 类

            /*
            * To change this template, choose Tools | Templates
            * and open the template in the editor.
            */
            package business;

            import java.io.Serializable;
            import java.util.ArrayList;

            /**
            *
            * @author One
            */
            public class UserPass implements Serializable {


                public String username;
                public String password;

                public ArrayList<UserPass> usernames;


                public UserPass(){
                    this.username ="";
                    this.password ="";  
                    this.usernames = new ArrayList<UserPass>();
                }

                public void setUserName(String username) {
                    this.username = username;

                }
                public String getUserName(){

                    return username;
                }

                public ArrayList<UserPass> getusernames(){

                return usernames;
            }
                public void setusernames(ArrayList<UserPass> a){

                    this.usernames = a;
                }
            }

尝试改变它告诉我财产不存在,据我所知。我肯定错了

提前致谢

4

1 回答 1

0

你有getusernames()哪些回报ArrayList,你没有提到setusernames(ArrayList<UserPass> )

但是,如果你想显示用户名,在 JSP 中,改变

<c:forEach var="user" items="${users}">

        <c:out value="${user.username}"/> 

</c:forEach>

这是如何工作的:

users是请求属性,它是一个List<UserPass>. 现在,当您使用 迭代它时forEach,您将UserPass进入user每一行。

然后从beanuser.username调用。getUserNameUserPass

在您的情况下user.usernames,它返回 empty Arraylist

于 2012-09-14T04:31:47.960 回答