你可以在地图中的排名存储与产品ID作为键,然后在jsp看起来像会话:
<c:forEach items="${products}" var="product">
<h2>${product.name}</h2>
${product.description} <br/>
<c:choose>
<c:when test="${not empty ratings[product.id]}">
${ratings[product.id].stars} <br/>
${ratings[product.id].comments} <br/>
</c:when>
<c:otherwise>
You have not rated this product
</c:otherwise>
</c:choose>
</c:forEach>
在您的文章的servlet /控制器:
Map<Long,Rating> ratings = new HashMap<Long,Rating>();
ratings.put(product.getId(),new Rating(1,"I think it's really bad.."));
session.setAttribute("ratings",ratings);
如果您有兴趣保持用户的评价或所有产品由Web应用程序管理,我会使用数据库而不是将数据存储在配置文件和会话中。考虑下面的数据库表:
产品:ID,描述等。
评级:ID,明星,评论,productId参数,用户id
用户:ID,姓名,电子邮件地址等。 ..
如果您使用数据库,请使用ORM工具,如Hibernate将您的表映射到对象。与上面所列的表你的对象应该是这样的:
public class Product {
Long id;
String description;
Set<Rating> ratings;
}
public class Rating{
Long id;
Integer stars;
String comments;
User user;
Product product;
}
public class User {
...
}
然后使用Hibernate,你会查询该产品,并获得所有与它的对象表单关联,为您一起工作的信息。您可以将必要的信息传递给视图。当然,你需要做一些配置来设置休眠状态。
您提到了MVC。你在使用MVC框架吗?我会研究Spring MVC或Struts。这将帮助您分离应用程序的各个层次。
你提到的流程似乎是正确的。 Servlet /控制器处理GET请求到主页面。从数据库中提取所有产品(或特定产品)以及他们的投票。将它们设置为参数。 JSP使用JSTL显示内容。
玩得开心!
谢谢你的详细回复...我现在没有使用任何特定的MVC框架,只是简单的Servlets/JSP等。 – xingyu 2012-03-29 05:07:19