1

我只想读出我之前在 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>

解决方案很可能非常简单。但我并没有真正得到我想要的。

4

2 回答 2

0

WHat you're doing makes no sense. You should see a JSP tag as a Java method, taking arguments. Once the method has been called, you can't get a handle on the method and ask it what the arguments were the last tie it was called.

The container will have a pool of BirolName instance, and will use them on several pages, in several threads.

And when you do

<jsp:useBean id="a" class="st.BirolName"/>,

it's equivalent to doing

st.BirolName a = new st.BirolName();

So you can't expect finding the arguments to the tag you used before from this new instance of the tag class.

When you pass a value to the instanzVar1 attribute of the tag, you're not setting any JSP attribute. You're just passing an argument to your tag. To set an page scope attribute, you must use the c:set tag:

<c:set var="someAttribute" value="someValue"/>
${someAttribute} <%-- prints someValue --%>
于 2012-11-26T23:18:54.323 回答
0

我找到了一个很好的解决方案。简单地在 .java 标记类中使用这段代码:

JspContext pcontext = getJspContext();    
pcontext.setAttribute("myAttribute", this.instanzVar1.concat(this.instanzVar1), Util.getScope("request"));

setAttribute() ”让我可以将值保存在属性中(此处为“myAttribute”)。描述:在我的例子中,我使用我的 taglib 属性在 JSP 中设置属性。然后我可以将它们设置在我的 .java 中,只需将它们作为 setAttribute 中的第二个参数放入(这里:“ this.instanzVar1.concat(this.instanzVar1 ”)。现在我有了我想要的,因为这个新属性“在 .java 中设置的 myAttribute" 可以在 JSP 中使用 ${myAttribute} 调用。

于 2012-11-28T11:08:20.093 回答