将多个值/结果集存储到java bean中

时间:2016-07-09 14:08:13

标签: java eclipse jsp javabeans rad

JSP代码

<form method="get" action="/asq/searchBox">

<select id="searchType" name="searchType">
<option id="username" value="username" >Username</option>
<option id="realName"  value="realName">Real name</option>
<option id="emailAddress" value="emailAddress">Email Address</option>
<option id="interest" value="interest">Interest</option>
</select>
<input type="text" name="searchBox" id="searchBox"/>
<br>
<input type="submit" name="submit" value="Submit" />
</form>

Java Bean

public class Friends {
    private String userId;
    private String firstName;
    private String lastName;
    private String displayname; 

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getDisplayname() {
        return displayname;
    }
    public void setDisplayname(String displayname) {
        this.displayname = displayname;
    }
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }

}

Servlet代码

String searchType = request.getParameter("searchType");
        String searchText = request.getParameter("searchBox");

        Friends searchBean = new Friends();

        if(searchType.equals("username") == true){
            try {

            //SQL Statement
            String sql = "SELECT firstName, lastName, userId, displayname FROM azq.registration WHERE azq.registration.displayname LIKE '%" + searchText+ "%'";
            // create the java statement
            Statement st = conn.createStatement();

            // execute the query, and get a java resultset
            ResultSet rs = st.executeQuery(sql);

            // iterate through the java resultset
            while (rs.next())
            {
                searchBean.setFirstName(rs.getString("firstName"));
                searchBean.setLastName(rs.getString("lastName"));
                searchBean.setUserId(rs.getString("userId"));
                searchBean.setDisplayname(rs.getString("displayname"));
            }

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    else if(searchType.equals("realName")== true){
        try {

            //SQL Statement
            String sql = "SELECT firstName, lastName, userId, displayname FROM azq.registration WHERE azq.registration.firstName LIKE '%" + searchText+ "%' OR azq.registration.lastName LIKE '%" + searchText+ "%'";
            // create the java statement
            Statement st = conn.createStatement();

            // execute the query, and get a java resultset
            ResultSet rs = st.executeQuery(sql);

            // iterate through the java resultset
            while (rs.next())
            {
                searchBean.setFirstName(rs.getString("firstName"));
                searchBean.setLastName(rs.getString("lastName"));
                searchBean.setUserId(rs.getString("userId"));
                searchBean.setDisplayname(rs.getString("displayname"));
            }

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    else if(searchType.equals("emailAddress")== true){
        try {

            //SQL Statement
            String sql = "SELECT firstName, lastName, userId, displayname FROM azq.registration WHERE azq.registration.emailAddress LIKE '%" + searchText+ "%'";
            // create the java statement
            Statement st = conn.createStatement();

            // execute the query, and get a java resultset
            ResultSet rs = st.executeQuery(sql);

            // iterate through the java resultset
            while (rs.next())
            {
                searchBean.setFirstName(rs.getString("firstName"));
                searchBean.setLastName(rs.getString("lastName"));
                searchBean.setUserId(rs.getString("userId"));
                searchBean.setDisplayname(rs.getString("displayname"));
            }

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    else if(searchType.equals("interest")== true){
        try {

            //SQL Statement
            String sql = "SELECT firstName, lastName, userId, displayname FROM azq.registration WHERE azq.registration.interest LIKE '%" + searchText+ "%'";
            // create the java statement
            Statement st = conn.createStatement();

            // execute the query, and get a java resultset
            ResultSet rs = st.executeQuery(sql);

            // iterate through the java resultset
            while (rs.next())
            {
                searchBean.setFirstName(rs.getString("firstName"));
                searchBean.setLastName(rs.getString("lastName"));
                searchBean.setUserId(rs.getString("userId"));
                searchBean.setDisplayname(rs.getString("displayname"));
            }

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    request.getSession(true).setAttribute("searchBean", searchBean);
    request.getRequestDispatcher("resultPage.jsp").forward(request, response);

}

检索代码

<jsp:useBean id="searchBean" class="sg.edu.nyp.sit.bean.Friends" scope="session"/>
<html>
<head>
<title>resultPage</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
    <jsp:getProperty name="searchBean" property="firstName" />
    <jsp:getProperty name="searchBean" property="lastName" />
    <br>
    <jsp:getProperty name="searchBean" property="displayname" />
</body>
</html>

我允许我的用户设置他们想要搜索的内容(例如用户名,电子邮件等)并输入他们的输入。然后在按下“提交”按钮后,他们将重定向到另一个页面,页面将显示他们搜索的内容。但是,我目前面临的问题是如何将结果集/多个值存储到java bean中并显示在另一个JSP页面中?

是否有其他解决方案正在使用JSTL功能?

1 个答案:

答案 0 :(得分:2)

您可以使用ArrayList来存储多个对象(Friends

建议您使用servlet作为View(JSP)和Model(Bean)之间的接口。因此,在模型中,您可以放置​​应用程序的业务逻辑

在你的Friends类中添加以下方法:

public List<Friends> loadList(String searchText) {
     List<Friends> Mylist = new ArrayList<Friends>();



    // ... Your code to fill the list 

  return Mylist;

    }

在每个while循环中填充arrayList,将一个Freinds对象添加到arrayList

while (rs.next())
            {
                searchBean.setFirstName(rs.getString("firstName"));
                searchBean.setLastName(rs.getString("lastName"));
                searchBean.setUserId(rs.getString("userId"));
                searchBean.setDisplayname(rs.getString("displayname"));

           Mylist.add(searchBean);

            }

在servlet中,调用此方法获取数据并将其传递给JSP:

// ...
    Friends searchBean = new Friends();

    List<Friends> list = searchBean.loadList(searchText);

        request.setAttribute("recolist", list);

        RequestDispatcher view = request.getRequestDispatcher("My.jsp");
        view.forward(request, response); 

在你的JSP中添加:

<c:forEach var="VAR" items="${recolist}">

${VAR./*WHAT YOU WANT*/}                                                         
</c:forEach>

请注意,VAR是一个Friends对象,您可以调用任何属性,例如:firstName,lastname ...