tags:

views:

744

answers:

3

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 ?

+1  A: 

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.

ChssPly76
Nice answer, shame it doesn't work here. What version of jspx does this for you ? Are you sure this isn't jsp ?
krosenvold
Sorry, it is indeed JSP; I've got them crossed. I suppose your way is the only one applicable then; c:if won't work in this case either.
ChssPly76
+1  A: 

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>
Alan Krueger
I didn't think c:if conditionals were permitted within element tags. Is that a change in later specifications ?
krosenvold
These jspx:element is not in the specification,it's a custom tag dropped by JSP2X.
Alan Krueger
*sigh* Wish you could edit these comments. =)
Alan Krueger
A: 

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"&gt;

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

Darren
Just use ternary operator in EL. `<option ${opt == selected ? 'selected' : ''}>`. No need for an overwhelming `<c:choose>` or `<c:if>` and code duplication.
BalusC
Have you actually tried this? It doesn't work - it's totally invalid XML.
Darren