This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

"target" version of c:set  RSS feed

 
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 : <c:out value="${???.dayName}"/>
Month Name : <c:out value="${???.monthName}"/>
BH : <c:out 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.
 
Marshal
Posts: 67226
170
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"/>
<c:out value="${someDay}"/><br>
works fine.

James.
 
James Leith
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry,
<c:out value="${someDay}"/><br>
should read
<c:out value="${doesntExistVar}"/><br>
 
Bear Bibeault
Marshal
Posts: 67226
170
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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!