【问题标题】:pass Object as JSTL tag attribute将 Object 作为 JSTL 标记属性传递
【发布时间】:2013-01-30 06:34:58
【问题描述】:

我正在尝试制作自定义标签。在这个标签中,我将传递一个对象作为属性,它应该返回一个数组列表。我已经尝试过了,我得到了一个例外。

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

我认为我的参数是作为字符串发送到类的,它无法将对象转换为给定类型。我该如何解决它并传递对象本身并在类中对其进行类型转换(因为 String 本身是一个类,我认为它可能,但我不知道该怎么做) 我的代码如下。 内页

内页

<%@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>

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>

ParentNodeOrder 类内部

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;
    }

}

【问题讨论】:

    标签: java exception object attributes jstl


    【解决方案1】:

    您遇到此异常是因为您的.tld 文件中的&lt;attribute&gt; 标记中缺少&lt;type&gt; 标记。
    &lt;type&gt; 标记是可选的,没有&lt;type&gt; 标记的属性被假定为String 数据类型。
    另外,您需要设置rtexprvalue = truertexprvalue 代表运行时表达式值。您需要将其值设置为 true,以便可以在运行时动态计算属性的值。
    所以你的attribute 声明在你的.tld 文件中应该是这样的:

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

    希望这会有所帮助!

    【讨论】:

    • 感谢您的回答。我已经包含了类型,但是我得到了同样的异常:( org.apache.jasper.JasperException: java.lang.ClassCastException: java.lang.String cannot be cast to com.showtable.helper.ParentNode
    • 那我觉得你还需要设置rtexprvalue = true!
    猜你喜欢
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 2013-12-13
    • 2015-02-25
    • 1970-01-01
    相关资源
    最近更新 更多