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?
The <%= blah %> syntax is not legal XML needed for JSP Documents. You have 2 options:
Hope that helps :)
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:
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 likeand have the jsp engine output
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.
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 hereI 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;
Using the scriptlet notation won't work for JSP documents (.jspx)
@alex great solution to use the ternary operator. I add some of my example, that thanks to you, I just changed the result of the condition, if true, writes the attribute, otherwise not write anything
to populate the list and select the value used, avoiding c:if
to check at start a radio button to avoiding c:if:
Make a tag library (.tagx) then use the scriptlet tag.
See http://code.google.com/p/jatl/wiki/JSPExample
Yeah this is cheating ... but it gets the job done. Plus can you do really nasty complicated recursion for tree structures this way.
I also posted another solution on my blog and gist.github that uses a bunch of tagx libraries: http://adamgent.com/post/8083703288/conditionally-set-an-attribute-on-an-element-with-jspx