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

HF page 446

 
Joe McIntyre
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the bottom of page 446, it says:

The "target" must evaluate to the OBJECT!


Can anyone tell me what this means? What does "the OBJECT" mean in this context?
[ October 19, 2004: Message edited by: Wally Flint ]
 
Roger Yates
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wally,

The OBJECT means a java object (a Map is a java object ).

Say you have an Employee class (not shown) which your servlet has instantiated with thisChap = new Employee("fred", 18); Your servlet then adds it to the request using setAttribute("myPerson", thisChap);
The OBJECT thisChap is now sitting in your request and is labelled "myPerson".

In your JSP that has been invoked by the servlet, you want to access fred's properties.
What you CANNOT do is this:
<c:set target="myPerson" property="name">NotFred</c:set>
since "myPerson" is just a string that you know happens to be an attribute. <c:set> doesn't know this. You might just as well put <c:set target="hi there, how are you?" ....>

Instead you must use an expression that evaluates to the object:
e.g. "${myPerson}"
Here, <c:set> will evaluate the EL expression, find the attribute "myPerson" and use the reference to the Employee object it gets back.
Whatever goes into target, <c:set> must be able to "evaluate" to get a reference.

In a nutshell, <c:set> will take whatever you pass in target=, and try to evaluate it. It can't evaluate a plain string, unless that string is an EL expression or scripting expression.

So in the example above, the OBJECT is an instantiation of the class Employee (which started with a name property of "fred"). To pass a reference to this object into c:set, we used an EL expression "${myPerson}" which when evaluted would return a reference to the object attribute the servlet added.

Does this help?
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, I am a bit confuse on the EL just you mentioned.

Sometimes, I saw ${variableX}, while sometimes I saw ${scope.variableX}. Thus, I wonder, which scope does ${variableX} refers to? Does the default is page scope, like <jsp:useBean>?

Nick
 
Joe McIntyre
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks!
 
Roger Yates
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick,

If you don't specify a scope e.g. ${variableX} then the EL evaluator will look in each scope until it finds a matching attribute called "variableX", starting with pageScope (as you suspected), then requestScope, sessionScope, and finally applicationScope (these are simply convenient Maps of attribute names and attributes). HFS p367 has a nice summary of this

Of course, if you really wanted the attribute stored in the session, and you wanted to be sure you didn't get one from a different scope, you could specify ${sessionScope.variableX} - the other syntax you've seen.

There is one "gotcha" when your attribute name has some form of separator, e.g. is called something like "my.variableX".
In this case, if you were to reference it, you'd try something like:
${pageScope.my.variableX}
EL would look for an attribute called "my" with property "variableX". Not what you want since this would fail. In this circumstance, you need to use EL's "[" operator. Simply used the expression ${pageScope["my.variableX"]} and you'll get what you wanted!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic