Conditionally set an attribute on an element with

2020-02-23 07:06发布

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?

9条回答
欢心
2楼-- · 2020-02-23 07:12

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 :)

查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-02-23 07:13

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>
<% } %>
查看更多
等我变得足够好
4楼-- · 2020-02-23 07:18

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.

查看更多
迷人小祖宗
5楼-- · 2020-02-23 07:20

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)

查看更多
混吃等死
6楼-- · 2020-02-23 07:23

@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

<select id="selectLang" name="selectLang" >
<c:forEach var="language" items="${alLanguages}" >
    <option value="${language.id}" ${language.code == usedLanguage ? 'selected' : ''} >${language.description}</option>
</c:forEach>

to check at start a radio button to avoiding c:if:

<input type="radio" id="id0" value="0" name="radio" ${modelVar == 0 ? 'checked' : ''} />
<input type="radio" id="id1" value="1" name="radio" ${modelVar == 1 ? 'checked' : ''} />
<input type="radio" id="id2" value="2" name="radio" ${modelVar == 2 ? 'checked' : ''} />
查看更多
何必那么认真
7楼-- · 2020-02-23 07:28

Make a tag library (.tagx) then use the scriptlet tag.

See http://code.google.com/p/jatl/wiki/JSPExample

<?xml version="1.0" encoding="UTF-8" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<jsp:directive.page import="com.googlecode.jatl.Html"/>
<jsp:directive.page import="com.evocatus.product.data.AttributeValue"/>
<jsp:directive.page import="com.evocatus.domain.Product"/>

<jsp:scriptlet>
//<![CDATA[
    final Product p = (Product) request.getAttribute("product");
        new Html(out) {{
           for (AttributeValue v : p.summaryAttributeValues()) {
                   p();
                        strong().text(v.getLabel()).end();
                        text(": " + v.getValue());
                   endAll();
           }
        }};
//]]>
</jsp:scriptlet>

</jsp:root>

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

查看更多
登录 后发表回答