Announcement Announcement Module
No announcement yet.
Handling session attributes like formBackingObject did Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Handling session attributes like formBackingObject did

    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:
    @RequestMapping(value = "/product/list.htm")
    @SessionAttributes(value = "productListForm")
    public class ProductListController {
    	private CategoryManager categoryManager;
    	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) {
    		return "redirect:/product/list.htm?" + request.getQueryString();
    	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;
    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.

    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.
    Last edited by whardwick; Apr 21st, 2010, 08:06 AM.

  • #2
    I modified the class/code above, I had accidentally pasted in the wrong class previously.
    Last edited by whardwick; Apr 21st, 2010, 08:07 AM.


    • #3
      Any thoughts that how you handled this issue ? I am facing the same issue.