1

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
keepmoving
  • 1,813
  • 8
  • 34
  • 74

2 Answers2

0

In second.jsp you've got the EL expression a little incorrect. What you're doing is calling the toString() method of itm and then appending ".name" to its output. Change

<c:out value="${itm}.name"></c:out>

to

<c:out value="${itm.name}"/>

Also change:

${param.nextItem}.name

to

${param.nextItem.name}
Asaph
  • 159,146
  • 25
  • 197
  • 199
  • I also tried it. but given following exception. org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/pages/other.jsp at line 8 5: 6: 7: Name *********** 8: – keepmoving May 27 '15 at 16:58
  • It is giving exception . exception org.apache.jasper.JasperException: org.apache.jasper.JasperException: javax.el.PropertyNotFoundException: Property 'name' not found on type java.lang.String – keepmoving May 27 '15 at 18:59
0

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>
zborek
  • 81
  • 8