我只想读出我之前在 JSP 中设置的属性(在 .tld 中)。我只是不知道如何输出它们。在尝试了如何使用 <c:out value=...> taglib一文中的建议后,我得到一个空值输出。所以变量(例如“instanzVar1”)似乎超出了范围。
这是我的.java:
public class BirolName extends SimpleTagSupport implements SimpleTag {
String instanzVar1;
Boolean instanzVar2;
int instanzVar3;
@Override
public void doTag() throws JspException, IOException {
//Zum Schreiben in die JSP "JSPWriter"
JspWriter outputJSP = getJspContext().getOut();
outputJSP.println("Birol was here :-) (Taglib von Java -nach-> JSP hat funktionniert!)");
outputJSP.println("Die eingegebenen Paramter über die JSP waren:");
outputJSP.println("<br /><b>");
outputJSP.println("Paramter 1:"+instanzVar1);
outputJSP.println("<br />");
outputJSP.println("Paramter 2:"+instanzVar2);
outputJSP.println("<br />");
outputJSP.println("Paramter 3:"+instanzVar3);
outputJSP.println("</b><br />");
}
public String getInstanzVar1() {
return instanzVar1;
}
public void setInstanzVar1(String instanzVar1) {
this.instanzVar1 = instanzVar1;
}
......
}
我的 .tld:
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
<tlib-version>1.0</tlib-version>
<short-name>st</short-name>
<tag>
<name>birolNameTag</name>
<tag-class>st.BirolName</tag-class>
<body-content>empty</body-content>
<attribute>
<name>instanzVar1</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>instanzVar2</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>instanzVar3</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
至少是.jsp:
<!-- Einbindung des Taglibs -->
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/st.tld" prefix="st"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<p>
Aufruf der Taglib mit "taglibPrefix:NameDesTag"
<br /> <st:birolNameTag instanzVar1="BirolString" instanzVar2="true" instanzVar3="1"/>
<br /><br />
<jsp:useBean id="a" class="st.BirolName"/>
Ausgabe des Paramters: <c:out value = "${a.instanzVar1}" />
</p>
解决方案很可能非常简单。但我并没有真正得到我想要的。