IBM WebSphere Commerce前台最近浏览商品重复显示问题及解决方法
Hey there, let's dig into fixing that annoying duplicate item issue in your WebSphere Commerce storefront's recently viewed module. I've worked through similar bugs with WC before, so here are the most likely causes and step-by-step fixes:
The most common culprit is that your code isn't checking for existing items before adding a new one to the recently viewed list.
By default, WebSphere Commerce uses the RecentlyViewedAddCmdImpl command to manage this list, but if you've customized it or the logic is broken, it might just append the same item every time you visit it.
To fix this, modify the command's performExecute method to check for duplicates first:
// Retrieve the current list of recently viewed items List<RecentlyViewedItemBean> viewedItems = getRecentlyViewedItems(); // Remove existing entry if the same product is already in the list Iterator<RecentlyViewedItemBean> itemIterator = viewedItems.iterator(); while (itemIterator.hasNext()) { RecentlyViewedItemBean existingItem = itemIterator.next(); if (existingItem.getCatalogEntryId().equals(newItem.getCatalogEntryId())) { itemIterator.remove(); break; } } // Add the new item to the top of the list viewedItems.add(0, newItem); setRecentlyViewedItems(viewedItems);
This ensures only unique items stay in the list, with the most recent one at the top.
Sometimes the issue comes from how the backend fetches the recently viewed data. Check the query that pulls data from the WC_USERACTIVITY table (or your custom storage):
- Look for duplicate entries in the table for the same session and product ID. If they exist, adjust the query to use
DISTINCTon the catalog entry ID, or add a condition to fetch only the most recent entry per product. - Verify that the session ID is correctly filtered—if the query is pulling data from multiple sessions by mistake, it might show duplicates.
Even if the backend list is correct, the JSP rendering might be looping incorrectly:
- In your storefront JSP (usually something like
RecentlyViewedDisplay.jsp), add debug prints to check the size of the viewed items list and each item's ID:<c:forEach var="item" items="${recentlyViewedItems}"> <!-- Debug line --> <p>Debug: Item ID = ${item.catalogEntryId}</p> <!-- Your existing display code --> </c:forEach> - If the debug output shows duplicate IDs, the problem is in the backend list. If the IDs are unique but the same product is displayed, check if the product lookup is broken (e.g., fetching the wrong product by ID).
Cached data can sometimes cause old, duplicate entries to stick around:
- Clear WebSphere Commerce's dynacache via the Admin Console > Cache Management.
- Flush the browser cache and test in incognito mode to rule out client-side caching.
- Restart the WebSphere Application Server to clear any in-memory session data that might be corrupted.
If you've customized the recently viewed module (e.g., added custom commands or modified view beans), double-check your code:
- Did you override the default
RecentlyViewedListBeanwithout preserving the deduplication logic? - Are there any custom event listeners or interceptors that are adding items to the list multiple times?
Compare your custom code against the default WC implementation to spot where the deduplication step might have been removed.
内容的提问来源于stack exchange,提问作者mohitkantgupta

