如何输出< option selected =“true”>>来自JSPX? [英] How to output <option selected="true"> from JSPX?
问题描述
几个html标签将给定属性的任意值解释为true - >选项标签让人想起。
我常常会这样做:
< C:选择>
< c:when test =$ {isSelected}/>
< option selected =true> Opt1< / option>
< / c:当>
< c:otherwise />
< option> Opt1< / option>
< / c:否则>
我知道我可以声明一个自定义来封装这个行为,但 变得非常难看,除非我用java编写它。
有没有更聪明的方法来做到这一点? 方法是使用自定义标签。
我喜欢JSP2X转换器采用的方法,在WEB-INF / tags文件夹中定义自定义标签让你做到这一点:
< jspx:element name =option>
< c:if test =$ {selected}>
< / c:if>
< jspx:body> Opt1< / jspx:body>
< / jspx:element>
更紧凑的方法可能是创建一个自定义标签,专门用于做出正确选择的选项,为所选属性取一个布尔值,如果它是真的,则发出selected =selected属性,否则不会。这将会更紧凑一些:
< jspx:option selected =$ {selected}> Opt1<选项>
A few html tags interpret "any" value of a give attribute as "true" -> option tags come to mind.
I frequently end up doing something like this:
<c:choose>
<c:when test="${isSelected}"/>
<option selected="true">Opt1</option>
</c:when>
<c:otherwise/>
<option>Opt1</option>
</c:otherwise>
</c:choose>
I know I can declare a custom to encapslate this behaviour but that also gets pretty ugly, unless I code it in java.
Is there a smarter way to do this ?
One way to approach this would be to use custom tag(s).
I like the approach that the JSP2X converter takes, defining custom tags in your WEB-INF/tags folder that let you do this:
<jspx:element name="option">
<c:if test="${selected}">
<jspx:attribute name="selected">selected</jspx:attribute>
</c:if>
<jspx:body>Opt1</jspx:body>
</jspx:element>
A more compact approach might be to create a custom tag specifically for an option that did the right thing, taking a boolean value for the selected attribute, emitting a selected="selected" attribute if it's true, not otherwise. This would be a bit more compact:
<jspx:option selected="${selected}">Opt1</option>
这篇关于如何输出< option selected =“true”>>来自JSPX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!