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

心不动则不痛 提交于 2019-12-05 11:13:15

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}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!