JSP 自定义标签属性的默认值 [英] Default value on JSP custom-tag attribute

查看:41
本文介绍了JSP 自定义标签属性的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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

有更好的方法吗?

推荐答案

还有更好的方法:

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

Java 和 tld 中不需要自定义标签.只是普通的 JSP EL 和条件运算符.

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

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

In my opinion it is shorter and cleaner than old:

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

这篇关于JSP 自定义标签属性的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆