user2486322
user2486322

Reputation: 867

JSP - Expression Language

I’m preparing for OCEWCD. Below code is appearing in a jsp page

<jsp:useBean id="acct1" class="aaa.bbb.Account"/>

<jsp:useBean id="acct2" class="aaa.bbb.Account " />

And

<jsp:setProperty name="acct2" property="address" value="${acct1.address}" />

This code is trying to copy ”address” attribute of “acct1” object to “acct2” object’s “address” attribute.

The explanation saying, the el expression (value="${acct1.address}") brings a reference to the address attribute of acct1 object.

But my understanding is, it will bring the string representation of the address attribute of acct1 object ( like- acct1.getAddress().toString() ).

Because if the following code appearing in a jsp page,

<%
Request.setAttribute(“A”, new A());
%>
${A}

the output might be something like

org.apache.jsp.newjsp_jsp$1A@1bcdccc3.

Explain me. Thanks

Upvotes: 0

Views: 372

Answers (1)

Pieter Kuijpers
Pieter Kuijpers

Reputation: 7317

The conversion to String takes place only when the object is printed in the output:

<% Request.setAttribute(“A”, new A()); %>
${A} <%-- A.toString() is called here --%>

In <jsp:setProperty name="acct2" property="address" value="${acct1.address}" /> nothing is printed to the output, so no string conversion takes places. In effect, this is the same as acct2.setAddress(acct1.getAddress())

Upvotes: 3

Related Questions