JSP custom-tag属性的默认值

时间:2022-04-15 09:03:24

When defining an attribute for a custom JSP tag, is it possible to specify a default value? The attribute directive doesn't have a default value attribute. Currently I'm making do with:

为自定义JSP标记定义属性时,是否可以指定默认值? attribute指令没有默认值属性。目前我正在做的事情:

<%@ attribute name="myAttr" required="false" type="java.lang.String" %>

<c:if test="${empty myAttr}" >
 <c:set var="myAttr" value="defaultValue" />
</c:if>

Is there a better way?

有没有更好的办法?

2 个解决方案

#1


48  

There is a better way:

有一个更好的方法:

<c:set var="title" value="${(empty title) ? 'Default title' : title}" />

No need for custom tag in java nor tld. Just plain JSP EL and conditional operator.

不需要java或tld中的自定义标记。只是简单的JSP EL和条件运算符。


In my opinion it is shorter and cleaner than old:

在我看来,它比旧的更短更干净:

<c:if test="${empty title}" >
 <c:set var="title" value="Default title" />
</c:if>

Regards

问候

#2


22  

So I wasn't able to figure out a way to add this to the attribute directive itself; it appears that the directive does not support this functionality. I was, however, able to create a tag that encapsulates the <c:if>...</c:if> logic. I had to write the tag in Java since there is no way (that I know of) to use an attribute value as a variable name.

所以我无法想出一种方法将它添加到属性指令本身;看来该指令不支持此功能。但是,我能够创建一个封装 ... 逻辑的标签。我不得不在Java中编写标签,因为我没有办法(我知道)使用属性值作为变量名。 :if>

First I wrote the tag file as a Java class:

首先,我将标记文件编写为Java类:

DefaultTag.java

DefaultTag.java

public class DefaultTag extends BodyTagSupport {

    private String var;
    private Object value;

    //for tag attribute
    public void setVar(String var) {
        this.var = var;
    }

    //for tag attribute
    public void setValue(Object value) {
        this.value = value;
    }

    public int doEndTag() throws JspException {
        Object oldValue = pageContext.getAttribute(var);
        Object newValue;

        if(value != null) {
            newValue = value;
        }

        else {
            if(bodyContent == null || bodyContent.getString() == null) {
                newValue = "";
            }

            else {
                newValue = bodyContent.getString().trim();
            }
        }

        if(oldValue == null) {
            pageContext.setAttribute(var, newValue);
        }

        else if(oldValue.toString().trim().length() == 0) {
            pageContext.setAttribute(var, newValue);
        }

        return EVAL_PAGE;
    }
}

Then I made a tld file:

然后我做了一个tld文件:

utils.tld:

utils.tld:

<?xml version="1.0" encoding="ISO-8859-1"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
        version="2.1">
    <tlib-version>2.0</tlib-version>
    <short-name>utils</short-name>
    <uri>http://utils</uri>
    <tag>
        <name>default</name>
        <tag-class>com.mystuff.mvc.tag.DefaultTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <name>var</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>value</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>

Then I made a custom tag that uses this tag:

然后我创建了一个使用此标记的自定义标记:

defaultTest.tag

defaultTest.tag

<%@ taglib prefix="utils" uri="/WEB-INF/tlds/utils.tld" %>
<%@ attribute name="value" required="true"%>
<%@ attribute name="optValue" required="false"%>

<utils:default var="optValue" value="optional monkeys"/>

${value} ${optValue}

After that I made a page to test the tag I just created:

之后我创建了一个页面来测试我刚创建的标签:

tagTest.jsp

tagTest.jsp

<mystuff:defaultTest value="helloThar" /><br/><br/>

<mystuff:defaultTest value="helloThere" optValue="monkeys" /><br/><br/>

<mystuff:defaultTest value="helloYou" optValue="${1 + 2 + 4 + 10}" /><br/><br/>

And that gave me:

那给了我:

helloThar optional monkeys

helloThar可选猴子

helloThere monkeys

你好猴子

helloYou 17

你好17岁

#1


48  

There is a better way:

有一个更好的方法:

<c:set var="title" value="${(empty title) ? 'Default title' : title}" />

No need for custom tag in java nor tld. Just plain JSP EL and conditional operator.

不需要java或tld中的自定义标记。只是简单的JSP EL和条件运算符。


In my opinion it is shorter and cleaner than old:

在我看来,它比旧的更短更干净:

<c:if test="${empty title}" >
 <c:set var="title" value="Default title" />
</c:if>

Regards

问候

#2


22  

So I wasn't able to figure out a way to add this to the attribute directive itself; it appears that the directive does not support this functionality. I was, however, able to create a tag that encapsulates the <c:if>...</c:if> logic. I had to write the tag in Java since there is no way (that I know of) to use an attribute value as a variable name.

所以我无法想出一种方法将它添加到属性指令本身;看来该指令不支持此功能。但是,我能够创建一个封装 ... 逻辑的标签。我不得不在Java中编写标签,因为我没有办法(我知道)使用属性值作为变量名。 :if>

First I wrote the tag file as a Java class:

首先,我将标记文件编写为Java类:

DefaultTag.java

DefaultTag.java

public class DefaultTag extends BodyTagSupport {

    private String var;
    private Object value;

    //for tag attribute
    public void setVar(String var) {
        this.var = var;
    }

    //for tag attribute
    public void setValue(Object value) {
        this.value = value;
    }

    public int doEndTag() throws JspException {
        Object oldValue = pageContext.getAttribute(var);
        Object newValue;

        if(value != null) {
            newValue = value;
        }

        else {
            if(bodyContent == null || bodyContent.getString() == null) {
                newValue = "";
            }

            else {
                newValue = bodyContent.getString().trim();
            }
        }

        if(oldValue == null) {
            pageContext.setAttribute(var, newValue);
        }

        else if(oldValue.toString().trim().length() == 0) {
            pageContext.setAttribute(var, newValue);
        }

        return EVAL_PAGE;
    }
}

Then I made a tld file:

然后我做了一个tld文件:

utils.tld:

utils.tld:

<?xml version="1.0" encoding="ISO-8859-1"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
        version="2.1">
    <tlib-version>2.0</tlib-version>
    <short-name>utils</short-name>
    <uri>http://utils</uri>
    <tag>
        <name>default</name>
        <tag-class>com.mystuff.mvc.tag.DefaultTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <name>var</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>value</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>

Then I made a custom tag that uses this tag:

然后我创建了一个使用此标记的自定义标记:

defaultTest.tag

defaultTest.tag

<%@ taglib prefix="utils" uri="/WEB-INF/tlds/utils.tld" %>
<%@ attribute name="value" required="true"%>
<%@ attribute name="optValue" required="false"%>

<utils:default var="optValue" value="optional monkeys"/>

${value} ${optValue}

After that I made a page to test the tag I just created:

之后我创建了一个页面来测试我刚创建的标签:

tagTest.jsp

tagTest.jsp

<mystuff:defaultTest value="helloThar" /><br/><br/>

<mystuff:defaultTest value="helloThere" optValue="monkeys" /><br/><br/>

<mystuff:defaultTest value="helloYou" optValue="${1 + 2 + 4 + 10}" /><br/><br/>

And that gave me:

那给了我:

helloThar optional monkeys

helloThar可选猴子

helloThere monkeys

你好猴子

helloYou 17

你好17岁