Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Custom tag lib help

 
Sam Drake
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My doubt is: How do I obtain a reference to a variable in the JSP page, which I have defined in my tag. eg:
I have a tag of the following fromat -
<mytag:tmvalue name="TM" value="value1" id="test" output="true" />
which outputs the value inside the TreeMap TM that is set in pageContext and also stores its value inside the attribute 'test'.
some code to explain further -
In my JSP I have done the following -
<%
TreeMap tm = new TreeMap();
tm.put("val1", "Hello");
tm.put("val2", "Hello World");
pageContext.getAttribute("TM", tm);
%>
<mytag:tmvalue name="TM" value="val1" id="test" output="true" />
My question is how to access 'test' variable directly in JSP.
Something like this -
<%=test%>
Is it possible and how?
I've tried to give pageContext.setAttribute("test", value); in my doStartTag() method of my TagHandler class but I'm still not getting it.
Error comes up as "test" is undefined.
Please help me to know how variables are stored in JSP and relationship with the pageContext object.
regards,
Sam.
 
Kaustubh Patil
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If you want to access the variables and print on the page them using the taglib, I suggest you have a look at the code of JSTL. In JSTL there is a custom tag 'C'. You can use <C:0UT> to print the var. on the page. Or better way if you can use the JSTL for all your needs it will solve most og your display problems.
Let me know if I have understood your problem correctly or you need something else.
-Kaustubh
[ September 24, 2003: Message edited by: Kaustubh Patil ]
 
Sam Drake
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
JSTL is a good option. I hadn't thouight about it. Guess I'll have to spend some time in learning the tags.
But the question is how are variables passed between tags and JSP if I want to pass them?
regards,
Sam.
 
Engin Okucu
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sam,
You have to define a class wich will extend the TagExtraInfo class.
to understandf it, here is a example that i found for you :
See here : Scripting Variables in JSP Tags
I hope it will help you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic