将多个 values/resultset 存储到 java bean
Storing multiple values/resultset into java bean
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豆子
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>
我允许我的用户设置他们想要搜索的内容(例如用户名、电子邮件等)并键入他们的输入。然后在按下提交按钮后,他们将重定向到另一个页面,该页面将显示他们搜索的内容。但是,我目前面临的问题是如何将 resultset/multiple 值存储到 java bean 中并显示在另一个 JSP 页面中?
是否有其他解决方案正在使用 JSTL 函数?
您可以使用 ArrayList
来存储多个对象 (Friends
)
建议您使用 servlet 作为 View (JSP) 和 Model(Bean) 之间的接口。所以在模型中你把你的应用程序的业务逻辑
在各位朋友class添加这个方法:
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 ...
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豆子
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>
我允许我的用户设置他们想要搜索的内容(例如用户名、电子邮件等)并键入他们的输入。然后在按下提交按钮后,他们将重定向到另一个页面,该页面将显示他们搜索的内容。但是,我目前面临的问题是如何将 resultset/multiple 值存储到 java bean 中并显示在另一个 JSP 页面中?
是否有其他解决方案正在使用 JSTL 函数?
您可以使用 ArrayList
来存储多个对象 (Friends
)
建议您使用 servlet 作为 View (JSP) 和 Model(Bean) 之间的接口。所以在模型中你把你的应用程序的业务逻辑
在各位朋友class添加这个方法:
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 ...