jsp自定义标签写法
程序员文章站
2022-07-15 09:21:57
...
页面引用:
tld:
java:
<%@ taglib uri="/WEB-INF/abc.tld" prefix="aa" %>
...
<aa:mytag .../>
tld:
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0">
<tlib-version>1.0</tlib-version>
<tag>
<name>adv</name>
<tagclass>com.mb.util.Advertisement</tagclass>
<attribute>
<name>placeId</name>
<required>true</required>
</attribute>
</tag>
</taglib>
java:
public class Advertisement extends TagSupport {
/** */
private static final long serialVersionUID = -1701876066227050270L;
/** 广告位ID */
private String placeId;
public int doEndTag() throws JspException {
String path = pageContext.getServletContext().getRealPath("/");
Long cityID = ((City) pageContext.getSession().getAttribute("city")).getId();
cityID = 1L;
// 得到广告位id
AdvdeployDAO dao = (AdvdeployDAO) BeanProxy.getBean("advDeployDAO");
AdvDeploy advdeploy = dao.getAdvdeploy("" + cityID, placeId, "0");
StringBuilder sb = new StringBuilder();
String advStr = "";
if (advdeploy != null) {
Adv adv = advdeploy.getAdv();
String advalt = "";
String imgurl = "";
Long advid = 0L;
if (adv != null) {
advalt = adv.getAlt();
imgurl = adv.getImgurl();
advid = adv.getId();
}
// 根据位置查看是不是滚动广告
Advplace ap = advdeploy.getPc().getAdvplace();
if (imgurl.endsWith(".swf")) {
sb.append("<embed src='#path#imgurl' width='#width' height='#height'/>");
} else {
sb.append("<a href='#path/updateAdvClickNum.mb?id=#id&cityID=#cityID' target='_blank'>");
sb.append("<img alt='#alt' src='#path#imgurl' width='#width' height='#height'/>");
sb.append("</a>");
}
advStr = sb.toString();
advStr = advStr.replace("#id", advid + "");
advStr = advStr.replace("#cityID", cityID + "");
advStr = advStr.replace("#alt", advalt);
advStr = advStr.replace("#path", path);
advStr = advStr.replace("#imgurl", imgurl);
advStr = advStr.replace("#width", ap.getWidth() + "");
advStr = advStr.replace("#height", ap.getHeight() + "");
}
JspWriter jw = pageContext.getOut();
try {
jw.println(advStr);
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY;
}
/**
* @return the placeId
*/
public String getPlaceId() {
return placeId;
}
/**
* @param placeId the placeId to set
*/
public void setPlaceId(String placeId) {
this.placeId = placeId;
}
}