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>");
}
}