jspobjectparameter-passingjspinclude

How to pass entity from parent JSP to included JSP


I am working on Spring MVC design, and its a very general requirement to pass object from One Jsp page to another Jsp Page. But I really do not know how to deal with it. I am doing something like this. Controller Code

Payment payment = new Payment();
        payment.setAmount(1000);
        payment.setName("naveen");
        Party party = new Party("Amit", "sharma");
        payment.setParty(party);

        Payment payment2 = new Payment();
        payment2.setAmount(2000);
        payment2.setName("naveen2");
        Party party2 = new Party("divanshu", "Nanlani");
        payment2.setParty(party2);

        Payment payment3 = new Payment();
        payment3.setAmount(3000);
        payment3.setName("naveen3");
        Party party3 = new Party("Pankaj", "chahal");
        payment3.setParty(party3);

        List<Payment> list = new ArrayList<Payment>();
        list.add(payment);
        list.add(payment2);
        list.add(payment3);
        ModelAndView mv = new ModelAndView();
        mv.addObject("model", payment);
        mv.addObject("list", list);
        mv.setViewName("success");

In controller Payment object is prepared with instance variable and Party reference.

one.jsp

<c:forEach var="item" items="${list}" varStatus="status">
    <jsp:include page="other.jsp">
        <jsp:param value="${list[status.index]}" name="nextItem"/>
    </jsp:include>

    <br/>

    <c:out value="${item.party.firstName}"></c:out>
    <br/>
</c:forEach>

Here I try to print the value of party object and it is printed successfully. But when pass the reference of payment object to another page called second.jsp. It is passed as string what I understand.

second.jsp

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

Name ------------ ${param.nextItem}.name (1)

<c:set value="${param.nextItem}" var="itm"></c:set>

Name ***********
<c:out value="${itm}.name"></c:out> (2)

In second.jsp page both line 1 and 2 print some thing like this

com.model.Payment@783bda.name

Can somebody guide me how to print the name or party.firstName on second Page.


Solution

  • jsp:param translate value into string. Try:

    <c:forEach var="item" items="${list}" varStatus="status">
        <c:set var="nextItem" value="${list[status.index]}" scope="request" />
        <jsp:include page="other.jsp" />
        ...
    </c:forEach>