Question

What's the right way to print an html attribute conditionally using JSPX?

These both throw validation errors on p tag:

    /* first try */

<p ${true ? 'name="foobar"' : ''}>hello</p>

    /* second one */

<c:set var="somevar" scope="page">
  <c:if test="${true}">
    name="foobar"
  </c:if>
</c:set>
<p ${somevar}>hello</p>

Element type "p" must be followed by either attribute specifications, ">" or "/>". at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:41)

EDIT: appended full code

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:form="urn:jsptagdir:/WEB-INF/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:spring="http://www.springframework.org/tags" version="2.0">
    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <jsp:output omit-xml-declaration="yes"/>

    <p ${true ? 'name="foobar"' : ''}>hello</p>

</div>
Was it helpful?

Solution

The problem is that Jasper tries to validate well-formness of JSP before processing EL.

This happens because JSPX extension that your file supposedly has means that it is a JSP Document. And JavaServer Pages Specification says:

It is a translation-time error for a file that is identified as a JSP document to not be a well-formed, namespace-aware, XML document.

I couldn't find any way to instruct Jasper to disable XML well-formness validation.

The Ant task to pre-compile JSP files as described in Tomcat docs has got validateXml parameter. But it just skips checks for a valid XML, not for well-formed XML.

So your options are either to rename your file to JSP, or add <is-xml>false</is-xml> to web.xml, or to follow @damo_inc's suggestion.

OTHER TIPS

A bit simplistic maybe, but should work:

      <c:if test="${true}">
        <p name="foobar">hello</p>
      </c:if>
      <c:if test="!${true}">
        <p>Hello</p>
      </c:if>

EDIT:

tested this:

<p ${true ? 'name="true"' : 'name="false"'}>hello</p>

...and it works fine. Something must be wrong with your page.

EDIT 2:

this works OK:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:c="http://java.sun.com/jsp/jstl/core" >
    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <jsp:output omit-xml-declaration="yes"/>

    <p ${true ? 'name="foobar"' : ''}>hello</p>

</div>

Something wrong with some of the xmnls attributes.

I did find a way to do it.

I know it has been a looong time since this was asked but thought someone could benefit from my finding.

I guess it is a complete hack, but it works.

Look at this:

&lt;div id="something1" <c:if test="true">class="hide"</c:if>&gt;
    something2
&lt;/div&gt;

With the &lt; and &gt; the tag is not validated.

The browser's source code shows:

<div id="something1" class="hide">
    something2
</div>

Got the idea from here.

Hope someone find it usefull

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top