Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on c:set target=""

 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In HFSJ on page 447 I am not able to understand what value is valid for target object.Here I am pasting the info as is on page 447

The target is for putting in an expression that resolves to the real object.If you put in a string literal that represents the "id" name of the bean or Map,it won't work.In other words "target" is not for the attribute name of the bean or map --- it's for the actual attribute object.


How can we reffer to the actual attribute ??? Can anyone please explain with an example by comparing what we set using setAttribute and what value is given in target?

Thanks
Veema
[ March 20, 2006: Message edited by: Veena Point ]
 
Ernesto Leyva
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are talking about a java-object stored as an "Attribute" in any of the scopes. So you can use a EL like this

<c:set targer="${javaObject} property="color" value="blue" />

In this line you are saying to the container to search in all scopes
for an attribute called javaObject but in fact the attribute is a reference
to a real java-object. So the code above is equivalent to say in java:


pageContext.findAttribute("javaObject").setColor("blue");

Somehow the container does the appropiate casting.
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What they mean by actual attribute object?even in jsp:usebean we use actual attribute object name? Does this mean the following?

Person p new Person();
p.setName("Test");
request.setAttribute("person"p);

<C:target target="person" property="name" value = "TestAgain">

in the above code target should be reffering to person as it will in id of jsp:useBean ?
 
Ernesto Leyva
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK if you have this:

Person p new Person();
p.setName("Test");
request.setAttribute("person"p);

and you do this
<C:target target="person" property="name" value = "TestAgain">

I think is equivalent to this java code:

person.setName("TestAgain");

But "person" does not have a valid reference at this point to anything. So it will throw exception.

But if you put this:

<c:target target="${person}" property="name" value = "TestAgain">

should be equivalent to this

pageContext.findAttribute("person").setName("TestAgain");

This should work provided the attribute "person" exist in any of the 4 scopes.
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.I understood to some extent.I tried the following code

Person p = new Person();
p.setName("TestName");
request.setAttribute("person",p);
%>
<%= p.getName()%>

<c:set target="${person}" property = "name" value = "MyTestNameAgain"/>

<%= p.getName()%>

it prints TestName TestName

it is not printing TestName MyTestNameAgain !
 
Ernesto Leyva
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's weird I think your code it should work.

You may try playing around with <jsp:useBean> or <jsp:getProperty> and see what you get. for the same code.

I will try that tonight and see what I get.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think you have not included the core tag library in your JSP. Include Standard tag library using

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> at the top of your page.It should give the proper result.

Thanks
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah ,I forgot to include taglib.

Thank you all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic