日期:2014-05-20 浏览次数:21178 次
package org.tag.image; import java.io.IOException; import javax.servlet.jsp.JspContext; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.JspFragment; import javax.servlet.jsp.tagext.SimpleTagSupport; public class ImageTag extends SimpleTagSupport { private JspFragment body; private Image image; public void setImage(Image image) { this.image = image; } public void doTag() throws JspException, IOException { JspContext jspCtx = getJspContext(); JspWriter out = jspCtx.getOut(); out.println("<img src="+image.getPath()+"/>"); body.invoke(null); } protected void getJspBody(JspFragment jspBody) { this.body = jspBody; } }
<tag> <name>logo</name> <tag-class>org.tag.image.ImageTag</tag-class> <body-content>tagdependent</body-content> <attribute> <name>image</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
<body> <jsp:useBean id="image" scope="page" class="org.tag.image.Image"></jsp:useBean> <%image.setName("123"); image.setPath("./style/image/bg0.jsp");%> <div id = "container"> <div id = "header"> <div id = "logo"> <my:logo image="image"></my:logo> </div> </div> </div> </body>
public class Image { private String name; private String path; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } }