JSPコードに複数の値/結果セットを格納のJavaビーン
<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;
}
}
サーブレットコード
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>
を取得
私はユーザーに検索対象を設定できるようにしています(例:ユーザー名、電子メールなど)を入力し、入力をキー入力します。 Submitボタンを押すと、別のページにリダイレクトされ、検索されたものがページに表示されます。しかし、私が現在直面している問題は、結果セット/複数の値をJava Beanに格納して別のJSPページに表示する方法です。
JSTL機能を使用している他のソリューションはありますか?
JSPにコレクションを表示するには、[方法の可能性の重複ページ](http://stackoverflow.com/questions/5241125/how-to-show-a-collection-in-a-jsp-page) –