I'm trying to introduce annotations in my spring application.
Take into consideration the following use case:
1. Click the "view categories" link, which brings you to a list of all categories
2. Click a "view products" link associated with a category, which brings you to a list of products that are assigned to the specified category
3. Click the "view categories" link again
4. Click the "view products" link of a DIFFERENT category than selected in step 2
Now, imagine that I put a List<Product> "productList" in the session at step 2. When I get to step 4, that session attribute doesn't get replaced. Instead, it shows all of the products for the category selected in step 2.
Here is the Controller:
Note that this controller displays a list of products, but also allows you to edit properties at the list level, hence the need for the session attribute.Code:@Controller @RequestMapping(value = "/product/list.htm") @SessionAttributes(value = "productListForm") public class ProductListController { @Autowired private CategoryManager categoryManager; @Autowired private ProductManager productManager; @ModelAttribute(value = "productListForm") protected ProductListForm initForm(@RequestParam(value = "cid", required = false) Integer categoryId) { List<Product> productList; if(categoryId != null) { Category cat = categoryManager.getCategory(categoryId); productList = new ArrayList<Product>(cat.getProducts()); Collections.sort(productList, new Product.Comparator(Product.Comparator.ALPHABETICAL_BY_NAME)); }else { productList = productManager.getProductList(new Product.Comparator(Product.Comparator.ALPHABETICAL_BY_CATEGORY_GROUP_NAME)); } return new ProductListForm(productList, categoryManager.getCategoryList(new Category.Comparator(Category.Comparator.ALPHABETICAL_BY_GROUP_NAME))); } @RequestMapping(method = RequestMethod.GET) protected String list() { return "list/productList"; } @RequestMapping(method = RequestMethod.POST) protected String submit(@ModelAttribute(value = "productListForm") ProductListForm productListForm, HttpServletRequest request, SessionStatus status) { productManager.getProductDAO().save(productListForm.getProductList()); status.setComplete(); return "redirect:/product/list.htm?" + request.getQueryString(); } @InitBinder protected void bind(WebDataBinder binder) { binder.registerCustomEditor(Category.class, new CategoryEditor(categoryManager)); } public void setCategoryManager(CategoryManager categoryManager) { this.categoryManager = categoryManager; } public CategoryManager getCategoryManager() { return categoryManager; } public void setProductManager(ProductManager productManager) { this.productManager = productManager; } public ProductManager getProductManager() { return productManager; } }
So, the question is, where is the best place to remove that session attribute "productList" when the page is navigated away from? You can see that when the list is saved, I call session.setComplete(), but I'm unsure of how to handle the other case.
Thank you for your response.


Reply With Quote
