• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tag file variable question

 
Ram Gokul
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Is jsp fragment on the exam ? (ie defining and use of jsp:invoke ? )

2. Can some one explain me the sample from SUn's site for <%@ variable...>

AT_BEGIN and name-from-attribute

In this example the AT_BEGIN scope is used to pass an EL variable to the tag's body and make to it available to the calling page at the end of the tag invocation. The name of the variable is specified via the value of the attribute var. The variable is referenced by a local name, result, in the tag file.

<%-- callingpage.jsp --%>
<c:set var="x" value="1"/>
${x} <%-- (x == 1) --%>
<my:example var="x">
${x} <%-- (x == 2) --%>
${result} <%-- (result == null) --%>
<c:set var="result" value="invisible"/> ----------AAAA
</my:example>
${x} <%-- (x == 4) --%>
${result} <%-- (result == `invisible') --%>
<%-- example.tag --%>
<%@ attribute name="var" required="true" rtexprvalue="false"%>
<%@ variable alias="result" name-from-attribute="var"
scope="AT_BEGIN" %>
${x} <%-- (x == null) --%>
${result} <%-- (result == null) --%>
<c:set var="x" value="ignored"/>
<c:set var="result" value="2"/>
<jsp oBody/>
${x} <%-- (x == `ignored') --%>
${result} <%-- (result == 2) --%> ------B
<c:set var="result" value="4"/>

The part i want to ask is when <jspdoBody/> is encountered in example tag, the tag file should have executed the body of the tag which has <c:set var="result" values="invisible"/>. Hwo can then result continues to be 2 ? ( I understand that result is a local variable ..but what i am trying to understand is the effect of <jsp oBody/>..a) does it have copy and paste functionality in which case <c:set> will be translated as part of example.tag
or b) does it have effect of local method ..in which case result is declared initialized and done with ..so that original value of result is maintained >


Bye the way he says "a tag file gets translated into a tag handler and then compiled. " where can i find this ?

Thanks in advance
[ July 26, 2006: Message edited by: Ram Gokul ]
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the above is a really good example.

Now : First let us see which variable is getting Synch for OUT.
It is a variable with alias "result" whose actual name is coming from attribute="var" . This attribute (IN) is passed the value "x" from the calling jsp. So now we know that it is the variable "x" which shall be synchornized.

OKay, Next a <%variable is OUT going.

Means if we were to change the value of "x" in the body-content of the tag in the jsp file - we would not be able to access this modified value in the Tag-file . Synch. takes place from Tag-file to the Jsp calling file. Not the other way round.

Another thing: "result" is NOT a variable that is getting synchronized.
It is the variable "x" that is getting synchronized on the invocation of <jsp:body/> and its modified value (if at all modified by tag file) being available in the calling jsp.

When we modify "result" in tag file - our variable "x" in jsp file gets modified.
When we modify "result" in jsp file it does not change the "result" variable in tag-file. WHY? Simply because the flow is from the Tag-file --> JSP and not the other way round.

So two points ::
1. Which variable is getting synch.
2. Where is the OUT flow going to. ( variable used for OUT attribute used for IN)

Restoration of value of "x" after the tag completes execution depends on the scope defined by the variable directive. If the scope is NESTED the value is restored to the initial state.

Hope this helps,
Regards,
Shivani.
[ July 26, 2006: Message edited by: Shivani Chandna ]
 
Ram Gokul
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks !!
So infer that the body content code has no impact on the tag file since the synching route is always from TAG to JSP file .

I have another clarification .

In the Tag file , changing the alias "result" variable has the same effect of changing the "x" variable.but what happens when 2 line change the same variable..

<c ut var="x" value=""/> -----AAA
<c ut var="result" value="...."/> ------BBB
....
{x} -- this must have printed the line BBB value
{result}-- this must have printed the line BBB value


but in the example it prints
{x}--- prints AAA
{result} -- printS BBB

So should we infer that once alias is used , modification to original variable will be ignored and only alias should be used all the time in the tag file.
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ram,

I directly used the variable "x"



in the tag-file and what I found was directly referencing the value of the attribute which is pointed by a variable alias is not modifying the calling jsp's variable.

Only using the alias is the modification taking place.


Regards,
Shivani
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic