Accesing annotated Spring session bean in JSP

My bean is annotated with

@Component("Person")
@Scope(value="session", proxyMode = ScopedProxyMode.TARGET_CLASS)

I have some getters and setters but the one I'm interested in to get in my JSP is

@Transient
    private ArrayList<DummyProduct> products = new ArrayList<DummyProduct>();

    public ArrayList<DummyProduct> getProducts() {
        return products;
    }

    public void setProducts(ArrayList<DummyProduct> products) {
        this.products = products;
    }

Then in my controller I add products to that list

DummyProduct prod = new DummyProduct(product);
this.person.getProducts().add(prod);

Then in my JSP I tried, with no luck to get the products:

<table id="cart_table" border="1">
<tr>
    <th>Product</th>
</tr>
<c:forEach var="prd" items="${sessionScope.Person.products}" >
<tr>
<td>${prd.productName}</td>
</tr>
</c:forEach>
</table>

I've also used the following property so that my beans are exposed to jsps: <beans:property name="exposeContextBeansAsAttributes"

<beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <beans:property name="prefix" value="/WEB-INF/views/" />
            <beans:property name="suffix" value=".jsp" />
            <beans:property name="exposeContextBeansAsAttributes" value="true" />

EDIT: The controller:

@org.springframework.stereotype.Controller
public class Controller {

@RequestMapping(value= "/addProduct", method = RequestMethod.POST)
    public String addProduct(HttpServletRequest request, Map<String, Object> model, @RequestParam String product){
        DummyProduct prod = new DummyProduct(product);
        this.person.getProducts().add(prod);
        return "loggedIn";
    }

}

Table is always empty. I've debugged and I saw that in my person bean, the products list is populated.

Upvotes: 0

Views: 290

Answers (1)

cralfaro
cralfaro

Reputation: 5948

The problem is related with the way you are accessing to the session attribute.

Change to this

UPDATED

@Controller
@SessionAttributes("person")

You can find an example here

Upvotes: 1

Related Questions