A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Java
»
JSP
Author
Programming HTML content inside Custom Tag Handler
Mark Reyes
Ranch Hand
Joined: Jul 09, 2007
Posts: 426
posted
Nov 23, 2010 21:00:13
0
Hi All,
I started exploring using custom tags and I am trying to experiment on some of its uses.
I declared my tld as this. My goal is that using my tag, I could display any collection class as HTML List Item
<tag> <description>Display List Data as HTML List Item</description> <name>displayAsList</name> <tag-class>com.test.UIDisplay</tag-class> <body-content>scriptless</body-content> <attribute> <name>listData</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
And use my tags like this.
<%@ taglib prefix="myTags" uri="simpleTags" %> <div> <b>Products</b> <myTags:displayAsList listData="${packages}">List of Products</myTags:displayAsList> </div>
The class that implements the tag is displayed below.
public class UIDisplay extends SimpleTagSupport { private List<String> listData; public void setListData(List<String> lstData) { this.listData = lstData; } public void doTag() throws JspException, IOException { JspWriter writer = getJspContext().getOut(); if (listData == null) { writer.println("Unable to find List of Products!"); throw new SkipPageException(); } getJspBody().invoke(null); writer.print("<ul>"); for (String strData : listData) { writer.print("<li>" + strData + "</li>"); } writer.print("</ul>"); } }
But I have a query, is this the only way to accomplish this task? It is as if I am programming HTML content on my
servlet
?
Sean Clark ---> I love this place!!!
Me ------> I definitely love this place!!!
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56185
13
I like...
posted
Nov 23, 2010 21:18:21
0
Tag files could be appropriate if you want to include
JSP
and HTML markup.
[
Smart Questions
] [
JSP FAQ
] [
Books by Bear
] [
Bear's FrontMan
] [
About Bear
]
Mark Reyes
Ranch Hand
Joined: Jul 09, 2007
Posts: 426
posted
Nov 24, 2010 02:30:08
0
Tag files could be appropriate if you want to include JSP and HTML markup.
Ahh so thats where Tag Files really comes into the picture. Makes sense actually...
Thanks guru!
Stefan Evans
Bartender
Joined: Jul 06, 2005
Posts: 1004
posted
Nov 24, 2010 14:02:29
0
One comment,
You would need to declare the type of your attribute in your tld as List.
ie:
<attribute> <name>listData</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.util.List</type> </attribute>
If you don't include the type tag, it will assume the default of
String
.
I agree. Here's the link:
http://zeroturnaround.com/jrebel
- it saves me about five hours per week
subject: Programming HTML content inside Custom Tag Handler
Similar Threads
mock question regarding dynamic attribute
Pass non string object as attribute to Tag File
String[] as attribute to custom tag
question about <body-content>
Jsp translation error
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter