views:

1971

answers:

6

In HTML forms, buttons can be disabled by defining the "disabled" attribute on them, with any value:

<button name="btn1" disabled="disabled">Hello</button>

If a button is to be enabled, the attribute should not exist as there is no defined value that the disabled attribute can be set to that would leave the button enabled.

This is causing me problems when I want to enable / disable buttons when using JSP Documents (jspx). As JSP documents have to be well-formed XML documents, I can't see any way of conditionally including this attribute, as something like the following isn't legal:

<button name="btn1" <%= (isDisabled) ? "disabled" : "" %/> >Hello</button>

While I could replicate the tag twice using a JSTL if tag to get the desired effect, in my specific case I have over 15 attributes declared on the button (lots of javascript event handler attributes for AJAX) so duplicating the tag is going to make the JSP very messy.

How can I solve this problem, without sacrificing the readability of the JSP? Are there any custom tags that can add attributes to the parent by manipulating the output DOM?

A: 

I don't really use JSP (and I replied once, then deleted it when I understood the "must by valid XML" thing). The cleanest I can come up with is this:

<% if (isDisabled) { %>
  <button name="btn1" disabled="disabled">Hello</button>
<% } else { %>
  <button name="btn1">Hello</button>
<% } %>
Marcus Downing
As stated in the question, while this will work it's messier than I would like, especially with 15+ attributes in common between the two branches of the if statement.
iainmcgin
A: 

The <%= blah %> syntax is not legal XML needed for JSP Documents. You have 2 options:

  1. Replace <%= (isDisabled) ? "disabled" : "" %> with <jsp.expression>(isDisabled) ? "disabled" : ""</jsp.expression>
  2. Use the Core taglib and EL (make sure isDisabled is put into page scope) like so:
<c:choose>
  <c:when test="${isDisabled}">"disabled"</c:when>
  <c:otherwise>""</c:otherwise>
</c:choose>

Hope that helps :)

Eric Wendelin
Where would these tags be placed though? You still wouldn't be able to put them within the attribute section of the button tag, as this would still be invalid XML, and placing them inside the element will just result in some CDATA being added.
iainmcgin
+1  A: 

Reading about an automatic jsp to jspx converter I came across the <jsp:element> and <jsp:attribute> tags. If I understand that correctly you should be able to do something like

<jsp:element name="button">
  <jsp:attribute name="someAttribute">value</jsp:attribute>
</jsp:element>

and have the jsp engine output

<button someAttribute="value"/>

or something like that. The only problem, pointed out in the page above, is that this doesn't seem to work well with conditional constructs. The author of the converter worked around that creating some helper tags, which you can have a look at downloading the source code I guess. Hope that helps.

agnul
Actually, that's probably <jspx:element> and so on. The JSP2X tool writes those as custom tags into your WEB-INF for use in converted JSP files.
Alan Krueger
+3  A: 

I use a custom JSP tag with dynamic attributes. You use it like this:

<util:element elementName="button" name="btn1" disabled="$(isDisabled ? 'disabled' : '')"/>

Basically, what this tag does is generate an XML element with elementName and puts all attributes present in the tag, but skips the empty ones.

The tag itself is pretty easy to implement, my implementation is just 44 lines long.

alex
Could you post the code for this? I think it would be very instructive.
Ian McLaird
This looks like it's going to be the best answer I'll get, it's a shame there's no way to do this with a valid XML syntax without having to write custom tags however...
iainmcgin
If you look at the JSP2X tool, it drops tags for <jspx:element>, <jspx:attribute>, and <jspx:body> (for the body of the element). That's another alternative that can be used with more complex constructions.
Alan Krueger
A: 

You can use the <jsp:text> tag to solve this problem using valid XML:

<jsp:text><![CDATA[<button name="btn1"]]></jsp:text>
    <c:if test="${isDisabled}"> disabled="disabled"</c:if>
    >
    Hello!
<jsp:text><![CDATA[</button>]]></jsp:text>

This is obviously more verbose than some other solutions. But it's completely self-contained: no custom tags required. Also, it scales easily to as many attributes as you need.

Bennett McElwee
+1  A: 

Hi all. i guess some time has passed since the last post on this, but I came up against the exact same problem with <select><option selected="selected"> tags, i.e. dynamically declaring which option is selected. To solve that one I made a custom tagx; I posted the details over in another answer here

I came to the conclusion that there is no nice shortcut; EL and JSP expressions can only exist inside XML element attributes (and in body content). So you have to do the following;

<c:choose>
    <c:when test="${isDisabled}"><button name="btn1" disabled="disabled">Hello</button></c:when>
    <c:otherwise><button name="btn1">Hello</button></c:otherwise>
</c:choose>

Using the scriptlet notation won't work for JSP documents (.jspx)

Darren