Hard coding HTML into JSP tag handler

T

todd.huss

At my company we're starting to use tag libraries a lot. A number of
the tag libraries have a fair amount of HTML hard coded in the java
code of the tag handler. Is this generally considered acceptable or a
"bad practice"?

Are there any best practices for separating HTML in a tag library so
that tag libraries themselves behave in an MVC like fashion?

Thanks!
 
C

Chris Smith

At my company we're starting to use tag libraries a lot. A number of
the tag libraries have a fair amount of HTML hard coded in the java
code of the tag handler. Is this generally considered acceptable or a
"bad practice"?

Are there any best practices for separating HTML in a tag library so
that tag libraries themselves behave in an MVC like fashion?

See JSP 2.0 tag files. Especially in conjunction with JSTL, they are
very useful for building up tags at a higher level of abstraction.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
T

todd.huss

I'm using JSP 2.0 tag libraries but my struggle is with what appears to
be the gross mixing of view (html) and controller (java) in one file.

With a .tag file I have to put a bunch of Java code into the view. With
a tag handler class I have to out.println a bunch of html from my java.
Is there no way to separate view and control in custom JSP tags?

Here's the example from an onjava article which is just appauling to me
because there's no separation of view and controller. It feels like a
regression back to the old servlet days when people would out.println
miles of HTML.

Does anyone have any suggestions on if there's a way to use a .tag file
for the HTML/EL and use a tag handler for the java code?

Thanks,
Todd

http://www.onjava.com/pub/a/onjava/2004/05/12/jsp2part4.html?page=last

package com.mycompany.mylib;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class PollTag extends SimpleTagSupport {
private String question;
private Map answers;
private String votesMapName;
private String answersMapName;

public void setQuestion(String question) {
this.question = question;
}

public void setAnswers(Map answers) {
this.answers = answers;
}

public void setVotesMapName(String votesMapName) {
this.votesMapName = votesMapName;
}

public void setAnswersMapName(String answersMapName) {
this.answersMapName = answersMapName;
}

public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
JspFragment body = getJspBody();
if (body != null) {
out.println("<p>");
body.invoke(null);
out.println("</p>");
}
out.print("Question:");
out.print(question);
out.println("<br>");
out.println("<form action=\"result.jsp\" target=\"result\">");
out.print("<input type=\"hidden\" name=\"question\" value=\"");
out.print(question);
out.println("\">");
out.print("<input type=\"hidden\" name=\"votesMapName\"
value=\"");
out.print(votesMapName);
out.println("\">");
out.print("<input type=\"hidden\" name=\"answersMapName\"
value=\"");
out.print(answersMapName);
out.println("\">");
Iterator i = answers.keySet().iterator();
while (i.hasNext()) {
String key = (String) i.next();
String value = (String) answers.get(key);
out.print("<input type=\"radio\" name=\"vote\" value=\"");
out.print(key);
out.print("\">");
out.print(value);
out.println("<br>");
}
out.println("<input type=\"submit\" value=\"Vote\">");
out.println("</form>");
}
}
 
C

Chris Smith

I'm using JSP 2.0 tag libraries but my struggle is with what appears to
be the gross mixing of view (html) and controller (java) in one file.

With a .tag file I have to put a bunch of Java code into the view. With
a tag handler class I have to out.println a bunch of html from my java.
Is there no way to separate view and control in custom JSP tags?

Yes, there is. Use a tag file, but don't put a bunch of Java code into
the tag file. Instead, use other tags and/or EL expressions in the tag
file. These other tags will express the logic, while the markup (HTML,
XML, WML, or whatever) stands in the tag file itself. These other tags
can be standard JSTL tags, or even your own custom tags written at a
lower level of abstraction.

There are other JSP 2.0 features that can help when implementing this
second set of tags, if you decide to write your own. Specifically, you
can provide entire sections of other JSP code as "fragments", which are
parameters to a parent tag. That allows you to put your logic in the
parent tag, while still leaving the markup in the tag file.
Does anyone have any suggestions on if there's a way to use a .tag file
for the HTML/EL and use a tag handler for the java code?

It's really fairly straight-forward. If you're having trouble seeing
how to apply it, can you give a specific example?

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
T

todd.huss

Thanks Chris, that sounds like the way to go. How do I go about using
my .tag file from within my tag handler class? I understand how to do
the reverse case (using the tag handler from within the .tag file since
that's how I would use jstl or any other taglib) but I would like to do
the opposite.

For example I would like my tag handler class to do the business logic
for getting the name of a user based on their user id stored in a
cookie. Then I would like to be able to call my .tag file from within
the tag handler passing it the users name to display.

Thanks,
Todd
 
C

Chris Smith

Thanks Chris, that sounds like the way to go. How do I go about using
my .tag file from within my tag handler class? I understand how to do
the reverse case (using the tag handler from within the .tag file since
that's how I would use jstl or any other taglib) but I would like to do
the opposite.

For example I would like my tag handler class to do the business logic
for getting the name of a user based on their user id stored in a
cookie. Then I would like to be able to call my .tag file from within
the tag handler passing it the users name to display.

You don't use a tag file from a tag handler. There's generally a one-
way path from JSP content (such as what you find in tag files) to Java
code. The only way to go the other direction is to use
RequestDispatcher includes and forwards, which are not well-suited for
your purpose.

The way to solve your general problem is to pass the additional JSP
content as either: 1) the body or 2) a parameter -- to the Java tag
handler. Option 1 is simple and doesn't even require JSP 2.0. Option 2
is JSP 2.0 only, since the concept of JSP fragments is new in that
version of the spec, and it's limited to specialized places where you
need several blocks of JSP content at your disposal for the tag logic.
So, the result can look like this:

Some text before the dynamic part.

<mylib:forUser>
Hello, there!

<c:choose>
<c:when test="${user.name == 'Chris'}">
That's a boring name.
</c:when>
<c:when test="${user.name == 'Jarika'}">
That's a weird name.
</c:when>
<c:eek:therwise>
Glad to meet you, ${user.name}.
</c:eek:therwise>
</c:choose>
</mylib:forUser>

Some text after the dynamic part.

I made the contents rather complex in order to make the point that any
arbitrarily complex JSP content can be placed there. You just need to
find a way to pass down the appropriate information from the parent tag.
For general purpose information, EL expressions are great for passing
information to the content of the tag. For more complex stuff that
doesn't reduce to standard EL types, you might choose something more
creative.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top