Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP Custom Tag Lib

 
Kumar Perumal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I follow the code which I got the code from developers.java.sun.com
http://developer.java.sun.com/developer/technicalArticles/javaserverpages/maintain_button/#author
Here I have paste my code files tld,class,jsp files as below
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.1</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>RTag</short-name>
<tag>
<name>RadioButton</name>
<tag-class>RadioBean.RadioButtonTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>RadioButtonSet</name>
<tag-class>RadioBean.RadioButtonSetTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>checked</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;

public class RadioButtonTag extends TagSupport {
public RadioButtonTag() {
}
private String value = null;
public void setValue(String value) {
this.value = value;
}
public int doStartTag() throws JspException {
RadioButtonSetTag tag = (RadioButtonSetTag)findAncestorWithClass(this, RadioButtonSetTag.class);
System.out.println("doStartTag()3");
if (tag == null) {
throw new JspTagException("Must be enclosed within RadioButtonSet");
}
try {
System.out.println("doStartTag()4");
pageContext.getOut().print("Radio Button Test");
pageContext.getOut().print("Kumar its working!!!");
//out.write("Helo world");
//pageContext.getOut().write("<input type=\"RADIO\" name=\"" + tag.getName()+ "\" value=\""+ value + "\" "+ tag.getHolder().getCheckStatus(value)+ ">");
System.out.println("doStartTag()5");
System.out.println("doStartTag()end2");
}
catch (Exception ex) {
throw new JspTagException("IO problems");
}
return SKIP_BODY;
}
}

package RadioBean;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class RadioButtonSetTag extends BodyTagSupport {
public RadioButtonSetTag() {
}
private String name = null;
private String checked = "button1";
private ButtonHolder holder = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public String getChecked(){
return checked;
}
public void setChecked(String checked) {
this.checked=checked;
}
public ButtonHolder getHolder() {
return holder;
}
public int doStartTag() {
System.out.println("doStartTag()1");
try{
holder = new ButtonHolder();
if (checked != null) {
System.out.println("doStartTag()2");
holder.setCheckedButton(checked);
}
}catch(Exception ex){
System.out.println("Error"+ex.getMessage());
}
return EVAL_BODY_AGAIN;
}
}

package RadioBean;
public class ButtonHolder {
public ButtonHolder() {
}
private static final String CHECKED = "";
private static final String UNCHECKED = "";
private String checkedButton = "button1";
private String[] checkedButtons = null;
private boolean radio = false;
public void setCheckedButton(String value) {
checkedButton = value;
System.out.println("Checked"+value);
radio = true;
}
public void setCheckedButtons(String[] values) {
checkedButtons = values;
radio = false;
}
public String getCheckedButton(){
return getCheckStatus("button1");
}
public String getCheckStatus(String value) {
if (radio) {
System.out.println("Radio");
if (value.equals(checkedButton)) {
return CHECKED;
} else {
return UNCHECKED;
}
} else {
if (checkedButtons == null) {
return UNCHECKED;
} else {
for (int i = 0; i < checkedButtons.length; i++) {
if (value.equals(checkedButtons[i])) {
return CHECKED;
}
}
return UNCHECKED;
}
}
}
}

<%@ page errorPage="Radio_error.jsp" %>
<%@ taglib uri="/WEB-INF/kp.tld" prefix="RTag" %>
<html>
<jsp:useBean id="bean0" class="RadioBean.ButtonHolder" />
<body>
<RTag:RadioButtonSet name="radioSet" checked="<%=bean0.getCheckStatus("")%>">
<RTag:RadioButton value="button1"> The first button </RTag:RadioButton>
<RTag:RadioButton value="button2"> The second button </RTag:RadioButton>
</RTag:RadioButtonSet>
<% out.println("Bye Bye "); %>
</body>
</html>

all the above code is working fine but Radio buttons are not appear
in the jsp screen
why?
Hope to hear from you soon...

Kumar
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kumar, welcome to the Ranch.
You'll get more out of these forums if you:
1) Use UBB code tags to keep your code formatted. Posting large blocks of unformatted code is a good way to get your question ignored.
2) Do some legwork on your own to try and narrow the problem down. Few people will have the time to set up all the code you posted to try and diagnose your problem. For example: have you looked at what is actually sent to the browser? Are the tags getting sent uninterpreted? Or is nothing getting sent at all? Are you getting any log messages? And so on.
bear
 
Kumar Perumal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the mail...
I understood and I keep your advise...
Yours,
Kumar
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just checking ... you know that this line is commented out?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic