I've been doing some Spring-based web forms work recently and one area of interest was around disabling certain input fields until particular values were set in the form-backing object. I wanted to ensure that particular radio buttons were disabled if the value was not what I expected.
Below is a statically disabled Spring radiobutton:
Technorati Tags: JSTL, JSP, Spring, HTML, Andrew Beacock
Below is a statically disabled Spring radiobutton:
<form:radiobutton path="name" disabled="true"/>For example's sake, I wanted to disabled the button if the name field of the form-backing object was "beacock", so what I needed was a boolean variable which captured the output of my particular test:
<c:if test="details.name eq 'beacock'">I could then use the above
<c:set var="isMe" value="true"/>
</c:if>
isMe
variable in the following:<form:radiobutton path="name" disabled="${isMe}"/>Simple huh? But you can go a step further in the strive for clean code, the
tag can take an optional var
parameter which is a holder for the outcome of the boolean expression:<c:if test="${details.name eq 'beacock'}" var="isMe"/>So now the whole thing becomes:
<c:if test="${details.name eq 'beacock'}" var="isMe"/>Although you can perform complex expressions in the
<form:radiobutton path="name" disabled="${isMe}"/>
${}
tags, you can use the ternary operator:value="${isMe ? 'Andrew Beacock' : 'Someone Else'}"Here are a few more things I've found you can use as well:
${!isMe}A very good reference for all things JSTL is the JSTL quick reference - an appendix of one of the Manning books. Also the TLD documentation of the Spring Form Tag library is full of all the optional stuff.
${not isMe}
${not isMe or someOtherVariable}
Technorati Tags: JSTL, JSP, Spring, HTML, Andrew Beacock
Comments