Conditional HTML attribute in JSPX

What is the correct way to print the html attribute conditionally using JSPX?

They both throw p tag verification errors:

/* 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> 

The element type "p" must be followed by the attribute specifications ">" or "/">. At org.apache.jasper.compiler.DefaultErrorHandler.jspError (DefaultErrorHandler.java:41)

EDIT: Added 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> 
+4
source share
3 answers

The problem is that Jasper is trying to validate the JSP before processing EL.

This is because the JSPX extension that your file supposedly has means that it is a JSP document. And the JavaServer Page Specification says:

This is a translation-time error for a file that is identified as a JSP document that does not have to be a well-formed XML document that supports a namespace.

I could not find any way to instruct Jasper to disable XML validation.

The Ant task for precompiling JSP files described in Tomcat docs has a validateXml parameter. But it just skips validation for valid XML , not well-formed XML .

So, your options are either to rename your file in JSP, or add <is-xml>false</is-xml> to web.xml , or execute the @damo_inc clause.

+3
source

A bit simplified, 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:

checked this:

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

... and it works great. Something is wrong with your page.

EDIT 2:

this works fine:

 <?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 is wrong with some xmnls attributes.

+1
source

I found a way to do this.

I know that it has been a long time since it was asked, but I thought that someone could benefit from my search.

I assume there 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; 

When &lt; and &gt; The tag is not validated.

The browser source code shows:

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

Got an idea here .

Hope someone finds this helpful.

0
source

Source: https://habr.com/ru/post/1397399/


All Articles