首页 > 解决方案 > 为什么 jasper 得到一个字符串而不是一个类对象?

问题描述

我正在尝试通过属性将 bean 对象传递给自定义标签。豆:

public class Statistic implements Serializable {
    private float average;
    // ...

    public Statistic() {
        this.average = 0;
        // ...
    }

    public float getAverage() {
        return average;
    }

    public void setAverage(float average) {
        this.average = average;
    }

    // ...
}

统计.tld

<taglib>
   <tlib-version>1.0</tlib-version>
   <jsp-version>2.0</jsp-version>
   <short-name>Statistic TLD</short-name>
   
   <tag>
      <name>Statistic</name>
      <tag-class>tags.StatisticTag</tag-class>
      <body-content></body-content>
      
      <attribute>
         <name>data</name>
         <required>true</required>
         <type>classes.Statistic</type>
         <fragment>false</fragment>
      </attribute>
   </tag>
</taglib>

统计标签

public class StatisticTag extends SimpleTagSupport {
    private Statistic statistic;
    // ...
    
       public void doTag()
       throws JspException, IOException {
          // ...
       }
       
       public Statistic getData() {
            return statistic;
        }
     
        public void setData(Statistic value) {
            this.statistic = statistic;
        }
}

JSP 文件

<%@ taglib prefix = "st" uri = "WEB-INF/statistic.tld"%>
<jsp:useBean id="statisticData" class="classes.Statistic" scope="page" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Index</title>
</head>
<body>
    <st:Statistic data="${statisticData}" />
</body>
</html>

如何将统计对象传递给 JSP:

request.setAttribute("statisticData", statistic);
getServletContext().getRequestDispatcher("/result.jsp").forward(request, response);

加载 result.jsp 时,出现错误:org.apache.jasper.JasperException: Unable to convert string "${statisticData}" to class "classes.Statistic" for attribute "data": Property Editor not registered with the PropertyEditorManager. 那么为什么 jasper 得到一个字符串而不是一个类对象呢?没有引号我得到一个错误org.apache.jasper.JasperException: /result.jsp (line: 9, column: 21) quote symbol expected

标签: javajsp

解决方案


推荐阅读