This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

how to pass Parameter in custom tag

 
Ranch Hand
Posts: 60
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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}
 
ashley Jug
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 67425
    173
    Mac Mac OS X IntelliJ IDE jQuery Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What are value1 and value2? Are they Integer instances?
     
    Bear Bibeault
    Marshal
    Posts: 67425
    173
    Mac Mac OS X IntelliJ IDE jQuery Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And I know I said this before, you should be using SimpleTagSupport rather than classic tags.
     
    Bear Bibeault
    Marshal
    Posts: 67425
    173
    Mac Mac OS X IntelliJ IDE jQuery Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 67425
    173
    Mac Mac OS X IntelliJ IDE jQuery Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And, why the getters?
     
    Bear Bibeault
    Marshal
    Posts: 67425
    173
    Mac Mac OS X IntelliJ IDE jQuery Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And, your doStartTag() signature does not override the base definition. It will never be called.
     
    Bear Bibeault
    Marshal
    Posts: 67425
    173
    Mac Mac OS X IntelliJ IDE jQuery Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 67425
    173
    Mac Mac OS X IntelliJ IDE jQuery Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    fName is a horrible name for a property. If it means first name, use firstName.

    You need a setter for all attribute properties.
     
    Ranch Hand
    Posts: 72
    Hibernate Eclipse IDE Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    ashley Jug
    Ranch Hand
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks a lot for all your help and advice. Issue resolved
     
    Don't touch me. And dont' touch this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic