Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass Object as JSTL tag attribute

I am trying to make a custom tag. In this tag, I will pass an Object as attribute and it should return an array list. I have tried it and I am getting an exception like.

org.apache.jasper.JasperException: java.lang.ClassCastException: java.lang.String cannot be cast to com.showtable.helper.ParentNode

I think my parameter is send as String to the class and there its not able to cast the object to the given type. How can I solve it and pass the Object itself and typecast it at the class(since String itself is an Class I think its possible, but i dont know how to do it) my code is given below. inside page

Inside page

<%@taglib prefix="test" uri="/WEB-INF/tlds/ShowTableCustomTag.tld"%>
<%
//the 'theMainObject' is of type ParentNode
request.setAttribute("mainobject", theMainObject);
%>
<test:getorder objectpara="mainobject"></test:getorder>

Inside ShowTableCustomTag.tld

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
  <tlib-version>1.0</tlib-version>
  <short-name>showtablecustomtag</short-name>
  <uri>/WEB-INF/tlds/ShowTableCustomTag</uri>
 <tag>
    <name>getorder</name>
    <tagclass>cc.showtable.customtag.ParentNodeOrder</tagclass>
    <info>tag to return order as arraylist</info>
    <attribute>
      <name>objectpara</name>
      <required>true</required>
    </attribute>
</tag>
</taglib>

Inside ParentNodeOrder class

package cc.showtable.customtag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import com.showtable.helper.ParentNode;
public class ParentNodeOrder extends TagSupport{

     private Object objectpara;

    @Override
    public int doStartTag() throws JspException {

        try {
            //Get the writer object for output.
            JspWriter out = pageContext.getOut();
            ParentNode parent=(ParentNode)objectpara;
            out.println(parent.getOrder());

        } catch (IOException e) {
            e.printStackTrace();
        }
        return SKIP_BODY;
    }
    public Object getObjectpara() {
        return objectpara;
    }
    public void setObjectpara(Object objectpara) {
        this.objectpara = objectpara;
    }

}
like image 611
arjuncc Avatar asked Jan 19 '26 21:01

arjuncc


1 Answers

You are experiencing this exception because you are missing the <type> tag inside your <attribute> tag in your .tld file.
The <type> tag is optional and an attribute without a <type> tag is assumed to be of String data type.
Also, you need to set rtexprvalue = true. The rtexprvalue stands for Runtime Expression Value. You need to set its value to true so that attribute's value can be dynamically calculated at runtime.
So your attribute declaration should be like this in your .tld file:

<attribute>
    <name>objectpara</name>
    <required>true</required>
    <type>com.showtable.helper.ParentNode</type>
    <rtexprvalue>true</rtexprvalue>
</attribute>

Hope this helps!

like image 139
S1LENT WARRIOR Avatar answered Jan 22 '26 10:01

S1LENT WARRIOR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!