Conditional HTML attribute in JSPX

只愿长相守 提交于 2019-12-07 11:30:15

问题


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>

回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/9359209/conditional-html-attribute-in-jspx

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