我创建了一个自定义标记,通过它我创建了一个锚标记。代码工作正常,但问题是它是三次创建锚标记。我不明白为什么会这样。 这是java代码
package test;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.*;
public class ImageTag extends SimpleTagSupport {
private String url = "";
private boolean bOpenNewWindow= false;
private String label= "";
private boolean close = false;
private String className = "";
private String title = "";
private boolean relAttribute = false;
public boolean isRelAttribute() {
return relAttribute;
}
public void setRelAttribute(boolean relAttribute) {
this.relAttribute = relAttribute;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public boolean isClose() {
return close;
}
public void setClose(boolean close) {
this.close = close;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public boolean isbOpenNewWindow() {
return bOpenNewWindow;
}
public void setbOpenNewWindow(boolean bOpenNewWindow) {
this.bOpenNewWindow = bOpenNewWindow;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public void doTag() throws JspException {
PageContext pageContext = (PageContext) getJspContext();
JspWriter out = pageContext.getOut();
StringWriter sw = new StringWriter();
Boolean isInternalUrl = getUrl().startsWith("/")?true:false;
//Boolean isContainHtml = getUrl().endsWith("html")?true:false;
try {
StringBuffer resultURL = new StringBuffer();
if(!getUrl().equals(""))
{
resultURL.append("<a href="+ url);
//this is internal URL and not poiniting to dam so need to append .html
if(isInternalUrl )
{
resultURL.append(".html\"");
}
// for appending classname
if( !getClassName().equals(""))
{
resultURL.append(" class=\""+ getClassName() +"\"");
}
// for appending Title attribute
if( !getTitle().equals(""))
{
resultURL.append(" title=\""+ getTitle() +"\"");
}
// for open link in new window
if(isbOpenNewWindow())
{
resultURL.append(" target=_blank");
}
if(isRelAttribute())
{
resultURL.append(" rel = nofollow");
}
resultURL.append(">");
// Label of link
if(!getLabel().equals(""))
{
resultURL.append(getLabel());
}
getJspBody().invoke(sw);
resultURL.append(sw.toString());
out.println(resultURL);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
这里是tld文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>testing</short-name>
<uri>http://www.tomcat-demo.com/testing</uri>
<description>This is a demonstration tag library</description>
<tag>
<name>link</name>
<tag-class>test.ImageTag</tag-class>
<body-content>scriptless</body-content>
<description>This tag is for displaying url</description>
<attribute>
<name>url</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>bOpenNewWindow</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<type>boolean</type>
</attribute>
<attribute>
<name>label</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>className</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>title</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>relAttribute</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
这里是jsp文件
<%@ page contentType="text/html; charset=ISO-8859-1"%>
<%@ taglib prefix="mytest" uri="http://www.tomcat-demo.com/testing"%>
<%
String name = "testttt";
pageContext.setAttribute("test" ,name);
%>
<div>
<span>
<mytest:link url="http://www.google.com" bOpenNewWindow="true" className="play" title="title of link" relAttribute = "true" >
<div class="hello">${test} </div>
</mytest:link>
</span>
</div>
这是我检查元素时的屏幕截图
有没有人对此有所了解
答案 0 :(得分:0)
您可以更改下面的代码
getJspBody().invoke(sw);
resultURL.append(sw.toString());
out.println(resultURL);
getJspBody().invoke(sw);
//resultURL.append(sw.toString());
//out.println(resultURL);
getJspContext().getOut().write(resultURL.toString());