GeeCON Prague 2014*
The moose likes JSP and the fly likes how to pass Parameter in custom tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "how to pass Parameter in custom tag" Watch "how to pass Parameter in custom tag" New topic
Author

how to pass Parameter in custom tag

ashley Jug
Ranch Hand

Joined: Sep 02, 2011
Posts: 60
Hello,

what is the syntax for passing parameter in custom tag?

For instance,

calculateTotal is defined in my tld file and it takes two numbers(num1,num2) and add them. what will be the syntax in the jsp to display the total using calculateTotal .

I know if no parameter is passed it will be:

<test:calculateTotal>

i have been looking for examples of how to pass parameter in custom tag but can' t get any.

Thanks a lot.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

Well, that depends upon how you have the attributes defined in the TLD.

By the way, is this just an academic exercise to learn how to use custom tags?

If not, it's like using nuclear weapons to crack walnuts when you can just add two values using the EL, as in: ${value1 + value2}


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ashley Jug
Ranch Hand

Joined: Sep 02, 2011
Posts: 60
Hello,

yes it is as an academic exercise to learn how to use custom tags.

I have done it using jstl also and it is very easy i agree with you and it is more to do with learning custom tag.

In my tld file it is defined as follows:


In my jsp it is defined as follows:


I am getting this error message:



Thanks in advance for your help.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

You'll notice the parameters are declared in the TLD as attributes. That's because you use attributes to pass them to the tag:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

You gave the attributes names for a reason!

<test:calculateTotal name="${value1}" num2="${value2}/>

[Edit: heh, Paul snuck in there before me!]

Your TLD also has a few issues:
  • The values should be required. Otherwise, how do you add when one or both of the values is missing?
  • Run-time expressions should be enabled or you will be stuck only being able to provide hard-coded values (as in Paul's example but not mine).

  • ashley Jug
    Ranch Hand

    Joined: Sep 02, 2011
    Posts: 60
    Hello,

    i have changed the tld file so that values are required and run time expression enabled. Now in my jsp the calculate tag is defined as follows:

    However when i execute my jsp i get the following errors:

    Although that i have the setter and getter as follows:


    Any suggestion please?
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61309
        
      66

    What are value1 and value2? Are they Integer instances?
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61309
        
      66

    And I know I said this before, you should be using SimpleTagSupport rather than classic tags.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61309
        
      66

    And, storing total as an instance variable is guaranteed to cause trouble.

    [edit: actually it looks like it's not used at all, what's it doing there?]
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61309
        
      66

    And, why the getters?
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61309
        
      66

    And, your doStartTag() signature does not override the base definition. It will never be called.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61309
        
      66

    At this point, I'd suggest you start completely over using SimpleTagSupport and don't add anything to the tag implementation that you don't understand the purpose of.
    ashley Jug
    Ranch Hand

    Joined: Sep 02, 2011
    Posts: 60
    Hello,

    i am using SimpleTagSupport now this is the flow of my tutorial:

    Basically i have a form which i insert a name:


    Then a Servlet that initialise the name:


    This is my tld file:

    This is my greeter.jsp page to display the name:



    This is my java class:


    However i don't know how to initialise fName variable of my above java class with the fName from greeter.jsp received from the servlet.

    Any suggestion please?



    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61309
        
      66

    fName is a horrible name for a property. If it means first name, use firstName.

    You need a setter for all attribute properties.
    Gopakumar Naryanan
    Ranch Hand

    Joined: Jan 15, 2011
    Posts: 72

    Think you might have solved the above problem with the help of SimpleTagSupport.

    I would like to show you the same with TagSupport:
    In my application there are 2 jsp files. One of them captures input (two numbers) from the user, keeps them in request Scope and forwards it to result page where i display the result using my <gopi:add/> tag.
    for that you need to change the following in the above addition of 2 numbers example:

    add.tld:
    =====
    <attribute>
    <name>num1</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
    <type>java.lang.Integer</type>
    </attribute>

    This is just a part of the tld file. i think you didn't specify the <type> in your previous tld.

    Result.jsp:
    =======
    <%@ taglib prefix="gopi" uri="http://example.com/tags/forms" %>
    <html>
    <body>
    Result : <gopi:add num2="${requestScope.num1}" num1="${requestScope.num2}"/>
    </body>
    </html>

    AddTag.java
    ========
    package demo.customtag;

    import java.io.IOException;

    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.JspWriter;
    import javax.servlet.jsp.tagext.TagSupport;

    public class AddTag extends TagSupport {

    private int num1;
    private int num2;

    public void setNum1(int num1) {
    this.num1 = num1;
    }

    public void setNum2(int num2) {
    this.num2 = num2;
    }

    public int doStartTag()throws JspException {
    JspWriter out=pageContext.getOut();
    System.out.println("addition : "+add(num1,num2));
    try {
    out.print(add(num1,num2));
    } catch (Exception e) {
    // TODO: handle exception
    }

    return SKIP_BODY;
    }

    public int doEndTag() throws JspException{
    return EVAL_PAGE;
    }

    public int add(int num1, int num2){
    return num1+num2;
    }
    }

    Hope this may give you a brief understanding.

    Thanks
    Gopakumar

    Thanks & Regards
    Gopakumar
    ashley Jug
    Ranch Hand

    Joined: Sep 02, 2011
    Posts: 60
    Thanks a lot for all your help and advice. Issue resolved
     
    GeeCON Prague 2014
     
    subject: how to pass Parameter in custom tag