我快抓狂了,明明为自定义标签中的成员变量写了get和set方法,
tomcat还是报”org.apache.jasper.JasperException: /taglib.jsp(9,0) Unable to find setter method for attribute: name“
------------------------------------------------------------------------
我测试的目的:希望通过自定义标签及其成员变量的值来在页面上显示符合条件的数据
我测试的环境:Tomcat5.0.12 使用JCreate编写代码
主要文件介绍:
MyTaglib.java //自定义标签处理类
mytaglib-product.tld //标签的配置文件
list.jsp //显示产品列表的JSP文件
------------------------------------------------------------------------
下面是各文件的内容:
-------------------------->>list.jsp<< --------------------------
<%@ page contentType="text/html;chartset=gb2312" language="java"%>
<%@ taglib uri="/WEB-INF/mytaglib-product" prefix="mytaglib" %>
<html>
<head>
<title>sdds</title>
</head>
<body>
<mytaglib:product name="Apple1"/><!--这里假设要显示商品名称为name1的信息-->
</body>
</html>
-------------------------->>MyTaglib.java<< --------------------------
package mytaglib;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.http.HttpSession;
import beans.BearingBean;
public class ProductTag extends TagSupport{
private String name=null;
public ProductTag(){
}
//看看看看看看,我在这里设置了getXXX和setXXX方法了
public String getName(){
return this.name;
}
public void setName(String name){
this.name=name;
}
public int doEndTag() throws JspTagException{
try{
//这里虚拟生成三件产品
Product[] bb=new Product[3];
for(int i=0;i<bb.length;i++){
bb[i]=new Product("Apple"+String.valueOf(i),
"price"+String.valueOf(i),
"type"+String.valueOf(i));
}//for
JspWriter out=pageContext.getOut();
//查找是否产品名称相等的。this.name.equals(bb[i].getName())
for(int i=0;i<bb.length;i++){
if(this.name.equalsIgnoreCase(bb[i].getName())){
//如果匹配就输出跳出循环。
out.print(bb[i].getName()+" "+bb[i].getPrice()+" "+bb[i].getType());
break;
}
}
}catch(Exception e){
throw new JspTagException(e.getMessage());
}
return EVAL_BODY_INCLUDE;
}
public void release(){
super.release();
}
}
-------------------------->>mytaglib-product.tld<< --------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<!--My taglib-->
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>mytaglib</shortname>
<uri>/mytaglib-product</uri>
<tag>
<name>product</name>
<tagclass>mytaglib.ProductTag</tagclass>
<bodycontent>JSP</bodycontent>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>