Conditionally set an attribute on an element with JSP Documents (JSPX)

拜拜、爱过 提交于 2019-12-03 19:09:24

问题


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?


回答1:


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.




回答2:


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.




回答3:


@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' : ''} />



回答4:


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)




回答5:


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.




回答6:


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




回答7:


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




回答8:


I've just been struggling with the same problem. I tried using <jsp:attribute name="disabled"/> inside <c:if>, but the compiler tries to attach the disabled attribute to the c:if element which fails. But I found that this does work (stripes:submit is an element for creating a button of type submit in stripes):

<stripes:submit name="process" value="Hello">
   <jsp:attribute name="disabled">
       <c:if test="${x == 0}">disabled</disabled>
   </jsp:attribute>
</stripes:submit>

It seems that jsp:attribute will not create an attribute at all if the body contains only whitespace, so you either get disabled="disabled" or nothing at all.

This will only work if you are using some sort of taglib to generate the button, and the tag element must support the disabled attribute (passing it through to the underlying HTML element). You can't use jsp:attribute to add an attribute to a raw HTML element.




回答9:


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>
<% } %>


来源:https://stackoverflow.com/questions/157005/conditionally-set-an-attribute-on-an-element-with-jsp-documents-jspx

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