option tags come to mind." />
开发者

How to output <option selected="true"> from JSPX?

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 sel开发者_StackOverflow中文版ected="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>


Yes, a smarter way would be to write

<option selected="selected">Opt1</option>

as that's what's mandated by XHTML.

I know that's not what you're really asking :-) I think your way is fine or you can use a conditional expression instead:

<option ${isSelected?"selected=\"selected\"":""}>Opt1</option>

It's shorter though not necessarily prettier.


For the <select><option selected="selected"> problem, I decided I wouldn't mind a bit of verboseness, if it was only one-time-verboseness... so I created a tag document (.tagx) in /WEB-INF/tags/select.tagx like so:

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
    xmlns:c="http://java.sun.com/jsp/jstl/core">

    <jsp:directive.attribute name="id" required="true" />
    <jsp:directive.attribute name="name" required="true" />
    <jsp:directive.attribute name="options" required="true" />
    <jsp:directive.attribute name="selected" required="true" />

    <select id="${id}" name="${name}">
    <c:forEach var="opt" items="${options}">
        <c:choose>
        <c:when test="${opt == selected}"><option selected="selected">${opt}</option></c:when>
        <c:otherwise><option>${opt}</option></c:otherwise>
        </c:choose>
    </c:forEach>
    </select>
</jsp:root>

and use it like so:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<html xmlns="http://www.w3.org/1999/xhtml" version="2.1"
    ...
    xmlns:form="urn:jsptagdir:/WEB-INF/tags/">

    ...

    <head>
        ...
    </head>
    <body>
        <form method="POST" commandName="loginRequest" action="index_login.html">
            <fieldset id="loginFieldSet">
                ...

                <div>
                    <label for="day" path="day">Favourite day: </label>
                    <form:select id="day" name="day" selected="Saturday"
                        options="Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday" />
                </div>
            </fieldset>
            <div>
            <input type="submit" tabindex="3" />
            <input type="reset" tabindex="4" />
            </div>
        </form>
    </body>
</html>

krosenvold, I don't agree that this is ugly... maybe annoying but I'm actually glad I didn't have to write any code for this. Once you've defined the tag, your JSPXs become much tidier. Besides, I simply don't think there is a short cut for this.


There is another way to get around this. It's a bit of a hack but an alternative to using a taglib or a choose where you duplicate the tag which I discovered works quite well.

You can build the tag inside a jstl set tag as the value, something like this:

<c:set var="mytag" value="&lt; option ${isSelected ? 'selected='\true\' : '' } &gt;">

Then, wherever you want that tag, you just output like this:

${mytag}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜