Java 如何在jsp中迭代以下代码?
我在Daoimpl中编写了一个方法,并将列表返回到我的jsp。有人能告诉我如何在我的jsp中迭代和打印列表的值吗 错误: Dao方法:在这个Dao方法中,我编写一个查询并将结果返回给jsp,有人告诉我如何打印这些值吗Java 如何在jsp中迭代以下代码?,java,hibernate,jsp,mapping,Java,Hibernate,Jsp,Mapping,我在Daoimpl中编写了一个方法,并将列表返回到我的jsp。有人能告诉我如何在我的jsp中迭代和打印列表的值吗 错误: Dao方法:在这个Dao方法中,我编写一个查询并将结果返回给jsp,有人告诉我如何打印这些值吗 public Map<String, Date> getTopBlogsQuesByDate()throws Exception { List<Blog> blogs = new ArrayList<Blog>(
public Map<String, Date> getTopBlogsQuesByDate()throws Exception {
List<Blog> blogs = new ArrayList<Blog>(0);
Map<String, Date> map = new HashMap<String, Date>();
try{
String sql = "select title , date from (select blog_title as title ,created_date as date from blog union select ask_question as title , created_on as date from askquestions ) as aa order by date desc";
SQLQuery query = getSession().createSQLQuery(sql);
query.setResultTransformer(AliasToBeanResultTransformer);
List l =query.list();
System.out.println("Total Number Of Records : "+l.size());
Iterator it = l.iterator();
while(it.hasNext())
{
Object o = (Object) it.next();
Blog b = (Blog)o;
b.setBlogTitle(b.getBlogTitle());
b.setCreatedDate(b.getCreatedDate());
map.put(b.getBlogTitle(), b.getCreatedDate());
System.out.println("Blog title Name : "+b.getBlogTitle());
System.out.println("Blog Date : "+b.getCreatedDate());
Askquestions a = (Askquestions)o;
a.setAskQuestion(a.getAskQuestion());
a.setCreatedQuesOn(a.getCreatedQuesOn());
System.out.println("Question title Name : "+a.getAskQuestion());
System.out.println("Question Date : "+a.getCreatedQuesOn());
map.put(a.getAskQuestion(), a.getCreatedQuesOn());
System.out.println("Unsorted HashMap: " + map);
TreeMap<String, Date> sortedHashMap = new TreeMap(map);
System.out.println("Sorted HashMap: " + sortedHashMap);
}
}
catch(Exception e){
e.printStackTrace();
}finally{
closeSession();
}
return map;
}
您尚未将类传递给别名ToBeanResultTransformer 改变
query.setResultTransformer(AliasToBeanResultTransformer);
到
另见
要在jsp中打印hashMap,可以使用JSTL forEach
它迭代hashmap,并返回EntrySet。然后可以使用它的getKey和getValue方法
注:
将jstl库添加到类路径,
添加您的jsp top。
您从数据库中提取的内容不是博客,而是一个简单的包含两个字段的对象数组。顺便说一句,这看起来不像JSP。您想在DOA java方法中打印sortedHashMap的内容吗?请注意,query.list会被调用两次。这里做了一些粗心的编程。@user2310289是的,我希望它在我的jsp中打印它们的值。正如我明确提到的,这不是我的jsp,而是我的dao方法。我有两个类,我在博客中问了一些问题。如何传递它们???@SalmanAmaan:不知道两个类,因为AliasToBeanResultTransformer只接受一个参数。我们可以传递吗他们分开吗??
query.setResultTransformer(AliasToBeanResultTransformer);
query.setResultTransformer(new AliasToBeanResultTransformer(Blog.class));
<c:forEach items="${myMap}" var="entry">
Key : <c:out value="${entry.key}"/> Value: <c:out value="${entry.value}"/> <br />
</c:forEach>