• Post Reply Bookmark Topic Watch Topic
  • New Topic

"target" version of c:set

 
James Leith
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,
I'm currently studying for the web component developer exam. using "Head
First Servlets and JSP" and I'm a bit confused by the "target" form of
<c:set>( page 446 ). The cause of the confusion is that rather than give
"target" an identifier like "id" in <jsp:useBean> you appear to have to
give it an object type like you would in Class.forName(). If I've got
that much right then I don't understand how I then get a reference to the
instance of the bean in order to access the property values. What do I do
if I want more than one of these beans in the same scope?

The following code might make this clearer:

In the uncommented section I create an instance of foo.JamesBean using
<jsp:useBean> and give it a reference name "jmlbean" which I can
subsequently use to access the values of the properties. I could, if I
wanted to use another <jsp:useBean> to create a second bean in the same
scope with a different name and refer to them separately.

In the commented out section I'd like to do exactly the same
thing using <c:set> but I don't have the equivalent of the <jsp:useBean>
"id" attribute.


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

<html>
<body>

Use jsp:UseBean to create a bean in page scope and print its contents...
<br>
<br>
<jsp:useBean id="jmlbean" class="foo.JamesBean" scope="page">
<jsp:setProperty name="jmlbean" property="dayName" value="Monday"/>
<jsp:setProperty name="jmlbean" property="monthName" value="May"/>
<jsp:setProperty name="jmlbean" property="bankHoliday" value="true"/>
</jsp:useBean>

<br>
<br>
Print using EL...
<br>
Day is : ${jmlbean.dayName}<br>
Month is : ${jmlbean.monthName}<br>
Bank Holiday is : ${jmlbean.bankHoliday}<br>
<br>
Print using jsp:getProperty...
<br>

Day is : <jsp:getProperty name="jmlbean" property="dayName"/><br>
Month is : <jsp:getProperty name="jmlbean" property="monthName"/><br>
BH is : <jsp:getProperty name="jmlbean" property="bankHoliday"/><br>

<%--

Even if I create a bean "jmlbean2" with <jsp:useBean> how
would I tell <c:set> to use that bean since I can't
give it an "id"?

<c:set value="Friday" target="${???}" property="dayName"/>
<c:set value="December" target="${???}" property="monthName"/>
<c:set value="false" target="${???}" property="bankHoliday"/>

Print the bean properties:<br>

Day Name : <cut value="${???.dayName}"/>
Month Name : <cut value="${???.monthName}"/>
BH : <cut value="${???.bankHoliday}"/>

--%>

</body>
</html>

Perhaps I've just missed the point of this tag. I have no problems
getting the "var" version of <c:set> to work and judging by the fact
that most of the online tutorials and documentation focus on the
"var" version maybe the "target" version is not widely used. I
appreciate that JSP should be presenting data rather than creating
it; however since it's on the exam I'd like to understand it.

Any advice would be greatly appreciated.

Thanks,
James.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Throughout all of that, it's hard to tell exactly what your question is. If the question is:

<c:set value="Friday" target="${???}" property="dayName"/>


what gets filled in for ??? then it is an EL reference to the bean to affected. In the case of your example, that would be:

 
James Leith
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear, that's what I was getting at, albeit in a rather circuitous way.
Is it right therefore to say that unlike the "var" form of <c:set>, the "target" version will not attempt to create a bean if the one specified does not exist already.
i.e. with reference to the original post
<c:set value="Friday" target="${jmlbean}" property="dayName"/>
works fine but
<c:set value="Friday" target="${jmlbean2}" property="dayName"/>
throws an exception
and
<c:set value="someval" var="doesntExistVar" scope="page"/>
<cut value="${someDay}"/><br>
works fine.

James.
 
James Leith
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry,
<cut value="${someDay}"/><br>
should read
<cut value="${doesntExistVar}"/><br>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, when using target with <c:set> the bean must already exist.
 
James Leith
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!