Conditional HTML attribute in JSPX

。_饼干妹妹 提交于 2019-12-05 18:09:16

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.

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.

elysch

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

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