在Java Bean Page中我写下这段代码: -
package sandip;
import java.sql;
import java.util.*;
import java.text.*;
import sandip.DataBaseConnection;
public class UserBean {
private ArrayList<Assay> assays = new ArrayList<Assay>();
private ArrayList<String> StringValue = new ArrayList<String>();
private Map<String, String> details = new HashMap<String, String>();
public void setDetails() {
Statement stmt = null;
ResultSet rs = null;
String sql = null;
DataBaseConnection obj = new DataBaseConnection();
Connection conn = null;
conn = obj.connectToDb();
try{
stmt = conn.createStatement();
sql = "SELECT `email`, `password` FROM `accounts` LIMIT 0, 12";
rs = stmt.executeQuery(sql);
while(rs.next()) {
details.put(rs.getString("password"), rs.getString("email"));
}
}catch(SQLException e) {
e.printStackTrace();
}catch(Exception ex) {
ex.printStackTrace();
}
}
public Map<String, String> getDetails() {
return this.details;
}
}
在我的jsp页面中,我用来迭代循环: -
但它没有打印任何东西。我现在应该改变什么?如果我还要做其他事情?
<h3>Hashtable iteration</h3>
<table border="1">
<c:forEach items="${userDetails.details}" var="item">
<tr>
<td align="center"><c:out value="${item.name}"/></td>
<td align="center">${item.value}</td>
</tr>
</c:forEach>
<br>
<ol>
<c:forEach items="${userDetails.details}" var="item">
<li><c:out value="${item.key}"/>=<c:out value="${item.value}"/></li>
</c:forEach>
</ol>
$ {item.key}不会打印任何值
答案 0 :(得分:0)
userDetails如何设置为JSP?谁在调用setDetails方法?
无论如何,拥有没有任何参数的setter方法并不是一个好主意,因为它违反了java bean约定。根据您的getDetails()
获取者,您的setDetails()
应接受Map<String, String>
作为参数。我只将UserBean
用作数据持有者,并将数据库提取代码移动到servlet(或其他控制器,服务或DAO对象)。