Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro JSP
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Pro JSP Advanced JSP coding questions. Beginning questions will be redirected to the Beginning JSP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro JSP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 8th, 2006, 02:50 AM
Registered User
 
Join Date: Feb 2006
Location: Bombay, Maharashtra, India.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to j.gohel
Default JSP SimpleTagSupport implementation problem

Hello,

I am developing tags which can generate Javascript at runtime.With classic tags i have done it succesfully but with simple tags one problem is coming

I have my jsp page like this:
Code:
<%@ taglib uri = "formtld.tld" prefix = "htm"%>

<html>
<title>Form Tag Built Using Simple Tag</title>

<body>
<htm:form name="empForm" >

    Name: <input type="text" name="username">
    <htm:validateField name="username" />

</htm:form>

</body>

</html>

Now in this jsp page my custom <form> tag handler functions fine but the nested tag doesn't work.What i want is a <form> tag is generated in the html's body tag & after that as a footer the script tag gets appended to the form tag in body ie of the sort:

html
body
form form attributes


close form
script tag

close script tag
close body
close html

KINDLY HELP ME OUT TO FIND OUT THE PROBLEM IN MY CODE:

The tag handler for outer tag 'form' is :
Code:
package simple;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;

public class SimpleTagForm extends SimpleTagSupport 
{
    private String name;  
    private String footer; 
    private String onsubmit;
    private MyLogger logger = MyLogger.getInstance();

    public SimpleTagForm()
    {
        name = null;
        footer = null;
        onsubmit = null;
    }

    public void setName(String name)
    {
        logger.log(" SimpleTagForm --> Inside setName()");
        this.name = name;
    }
    public String getName()
    {
        return name;
    }
    public void setFooter(String footer)
    {
        logger.log(" SimpleTagForm --> Inside setFooter()");
        this.footer = footer;
    }
    public String getFooter()
    {
        return footer;
    }
    public void setOnSubmit(String onsubmit)
    {
        logger.log(" SimpleTagForm -->Inside setOnSubmit()");
        this.onsubmit = onsubmit;
    }
    public String getOnSubmit()
    {
        return onsubmit;
    }

    public void doTag() throws JspException,IOException
    {
        logger.log(" SimpleTagForm --> doTag()");
        Writer out = getJspContext().getOut();
        JspFragment fragment = getJspBody();

        String formTag = createFormTag();
        out.write(formTag);
        fragment.invoke(out);

    }

    public String createFormTag()
    {
        logger.log("SimpleTagForm --> createFormTag()\n");

        StringBuffer sbf = new StringBuffer(""); 
        sbf.append("<form");

        if(name != null)
        {
            logger.log("SimpleTagForm --> createFormTag()-->name not null\n");
            sbf.append(" name=\"").append(name).append("\" ");
        }

        if(onsubmit != null)
        {
            logger.log("SimpleTagForm --> createFormTag()--> onsubmit not null");
            sbf.append(" onsubmit=\"").append(onsubmit).append("\"");
        }

        sbf.append(">");
        sbf.append("\n</form>\n");

        if(footer != null)
        {
            logger.log("SimpleTagForm --> createFormTag()-->footer not null\n");
            sbf.append(footer);
        }

        return (sbf.toString());

    }


}
& the nested tag handler FOR VALIDATE TAG is :
Code:
package simple;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;


public class SimpleTagValidate extends SimpleTagSupport 
{
    private String name;
    private MyLogger logger = MyLogger.getInstance();


    public SimpleTagValidate()
    {
        name = null;
    }

    public void setName(String name)
    {
        this.name = name;
    }
    public String getName()
    {
        return name;
    }

    public void doTag() throws JspException,IOException
    {
        logger.log("Inside doTag() of SimpleTagValidate");

        SimpleTagForm simpleFormTag = (SimpleTagForm)findAncestorWithClass(this,simple.SimpleTagForm.class);

        String onsubmit = simpleFormTag.getOnSubmit();
        String formName = simpleFormTag.getName();
        String footer = simpleFormTag.getFooter();


        if(onsubmit == null)
        {
            logger.log("SimpleTagValidate : Inside if onsubmit is null");

            onsubmit = "return ";
            onsubmit = onsubmit + formName + "_fieldsReq(this)" ;
            footer = "<script type=\"text/javascript\">\n";
            footer = footer + "function ";
            footer = footer + formName + "_fieldsReq(theForm)\n{\n";

            footer = footer + getVerified();

            footer = footer + "}\n";
            footer = footer + "</script>\n";

        }

        simpleFormTag.setOnSubmit(onsubmit);
        simpleFormTag.setFooter(footer);

    }

    public String getVerified()
    {
        logger.log("SimpleTagValidate : Inside getVerified()");
        StringBuffer sbf = new StringBuffer("");
        sbf.append("\t if (!theForm.").append(name).append(")\n");
        sbf.append("\t { \n");
        sbf.append("\t \t alert('Could not get field ").append(name).append("'); \n");
        sbf.append("\t \t return false;\n");
        sbf.append("\t }\n");
        sbf.append("\t if(theForm.").append(name).append(".value == '')\n");
        sbf.append("\t { \n");
        sbf.append("\t \t alert('").append("Please enter value for the field "+name).append("');\n").append("\t \t return false;\n");
        sbf.append("\t }\n");
        return (sbf.toString());

    }
}
& the tld for the same is:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE taglib
    PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
    "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">



<taglib>

    <tlibversion>1.0</tlibversion>
    <jspversion>1.1</jspversion>
    <shortname>SimpleTagFormTag</shortname>
    <uri>formtld</uri>


    <tag>
        <name>form</name>
        <tagclass>simple.SimpleTagForm</tagclass>
        <bodycontent>scriptless</bodycontent>


        <attribute>
            <name>name</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>


    </tag>

    <tag>
        <name>validateField</name>
        <tagclass>simple.SimpleTagValidate</tagclass>
        <bodycontent>empty</bodycontent>


        <attribute>
            <name>name</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>


    </tag>


</taglib>
So please help me out.

Thanks
Jignesh

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with EL in JSP IronStar JSP Basics 1 January 31st, 2007 11:54 PM
jsp:forward problem rushman Pro JSP 1 October 24th, 2005 10:36 AM
IEWebBrowser2 implementation problem... fred_213 C++ Programming 0 March 10th, 2005 02:37 PM
JSP encoding problem recepkocur Apache Tomcat 0 December 3rd, 2004 02:44 AM
JSP encoding problem! recepkocur Pro JSP 0 December 3rd, 2004 02:43 AM



All times are GMT -4. The time now is 09:06 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.