Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to pass Parameter in custom tag

 
ashley Jug
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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.
 
Paul Clapham
Sheriff
Posts: 21416
33
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
Author and ninkuma
Marshal
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
    Author and ninkuma
    Marshal
    Pie
    Posts: 65226
    95
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What are value1 and value2? Are they Integer instances?
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 65226
    95
    IntelliJ IDE Java jQuery Mac Mac OS X
    • 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
    Author and ninkuma
    Marshal
    Pie
    Posts: 65226
    95
    IntelliJ IDE Java jQuery Mac Mac OS X
    • 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
    Author and ninkuma
    Marshal
    Pie
    Posts: 65226
    95
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And, why the getters?
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 65226
    95
    IntelliJ IDE Java jQuery Mac Mac OS X
    • 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
    Author and ninkuma
    Marshal
    Pie
    Posts: 65226
    95
    IntelliJ IDE Java jQuery Mac Mac OS X
    • 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
    Author and ninkuma
    Marshal
    Pie
    Posts: 65226
    95
    IntelliJ IDE Java jQuery Mac Mac OS X
    • 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.
     
    Gopakumar Naryanan
    Ranch Hand
    Posts: 72
    Eclipse IDE Hibernate 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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic