你可以在地圖中的排名存儲與產品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