代码之家  ›  专栏  ›  技术社区  ›  Vivin Paliath

JSP自定义标记属性的默认值

  •  47
  • Vivin Paliath  · 技术社区  · 14 年前

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

    <%@ attribute name="myAttr" required="false" type="java.lang.String" %>
    
    <c:if test="${empty myAttr}" >
     <c:set var="myAttr" value="defaultValue" />
    </c:if>
    

    2 回复  |  直到 6 年前
        1
  •  51
  •   halfer    6 年前

    有更好的方法:

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

    在Java和TLD中不需要自定义标记。简单的JSP EL和条件运算符。


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

    <c:if test="${empty title}" >
     <c:set var="title" value="Default title" />
    </c:if>
    
        2
  •  22
  •   Vivin Paliath    14 年前

    所以我想不出一个方法把这个添加到 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> 逻辑。我必须在爪哇写标签,因为我不知道如何使用属性值作为变量名。

    首先,我把标记文件写成Java类:

    Debug

    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;
        }
    }
    

    然后我做了一个 tld 文件:

    UTL.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>
    

    然后我做了一个使用这个标签的自定义标签:

    默认测试标记

    <%@ 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}
    

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

    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/>
    

    海洛萨可选猴子

    猴子们

    你好17