【发布时间】:2014-07-14 02:33:57
【问题描述】:
我是 jsp 自定义标签的新手。我想要的是使用自定义标签在我的 jsp 页面中打印 ArrayList。
我的自定义标签的 .tld 文件在下面,
<tag>
<name>HotelDetails</name>
<tag-class>it.testproject.custom.HotelDetails</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>id</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
<attribute>
<name>hotelArray</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.util.ArrayList</type>
</attribute>
</tag>
这里我使用了属性来获取arraylist。我可以在jsp页面中访问它。
我的标签处理程序类看起来像,
package it.testproject.custom;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class HotelDetails extends BodyTagSupport
{
private static final long serialVersionUID = 1141701851233089895L;
private Hotel hotel;
private ArrayList<Hotel> hotelArray;
City regionCity;
Iterator<Hotel> it;
public ArrayList<Hotel> getHotelArray()
{
return hotelArray;
}
public void setHotelArray( ArrayList<Hotel> hotelArray )
{
this.hotelArray = hotelArray;
it=hotelArray.iterator();
System.out.println( "[HotelDetails] Hotels : " + hotelArray.size() );
}
@Override
public int doStartTag() throws JspException
{
hotel=it.next();
pageContext.setAttribute( "hotelName", hotel.getName() );
pageContext.setAttribute( "hotelDesc", hotel.getDescription() );
pageContext.setAttribute( "hotelRating", hotel.getStarRating() );
pageContext.setAttribute( "defaultPrice", hotel.getTotalPriceWithTax() );
System.out.println(hotel.getName()+" "+hotel.getStarRating());
// pageContext.setAttribute( "hotelDesc", hotel.getDescription() );
// pageContext.setAttribute( "hotelRating", hotel.getStarRating() );
// pageContext.setAttribute( "hotelMainImage", hotelImageBase + hotel.getMainImage().getImageURL() );
return EVAL_BODY_INCLUDE;
}
}
我返回了 EVAL_BODY_INCLUDE,它只得到一个结果。我在一些关于 EVAL_PAGE 的帖子中看到自动调用 doStartTag() 。我试过了,但没有结果。请帮忙。
【问题讨论】: