为什么ArrayList返回空字符串[] [] []?

时间:2012-09-13 21:26:58

标签: jsp java-ee servlets web

这是我的数据库查询功能

        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;
                }
            }

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

提前致谢

1 个答案:

答案 0 :(得分: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

然后user.usernamegetUserName bean调用UserPass

在您的案例user.usernames中,它返回空Arraylist