How to concatenate string literals inside a JSP expression in a custom jsp tag

I have a really weird problem following the jsp tag attribute

 <custom:tag onclick="addBid('<%= container_index + "string" %>');" />

cannot be processed by jsp compiler

20:18:00,374 ERROR [render_portlet_jsp:154] org.apache.jasper.JasperException: /WEB-INF/jsp/customers/abcd.jsp(146,107) equal symbol expected
        at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)

Simply, if there are double quotes around " '<%= %>' ", they cannot be inside again" '<%= " " %>' "

On the other hand, if it was in an html element:

<input id="bid" onclick="addBid('<%= container_index + "string" %>');" />

It works great

Please do not tell me that I should use tag libraries for this ... :-)

+3
source share
4 answers

First of all in JSP

<%=varName%> 

scriptlet means: change line a) with

varName.toString()

It is so strange that you want to output a variable whose name is unknown.

This is similar to Java. You write

String aVariableString = "test String";
System.out.println(aVariable+"String");

This makes no sense.

However, I can provide a similar code for yours depending on the situation:

, :

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!-- head, or anything You want -->
<script>
    function addBid(){
        var bidId = document.getElementById("bidId").value;
        // do whatever with bidId
    }
</script>

<input type="hidden" id="bidId" value="<c:out value=${containerIndexes[knownIndex]}" />    

<custom:tag onclick="addBid();" />

, , :

<c:forEach var="bid" items="${bids}">
    <c:out value=${bid.name} /> <custom:tag onclick="addBid(${bid.index});" />
</c:foreach>

bid, , ,

getName() 

getIndex() 

.

JSP ,

<custom:tag onclick="addBid('${bid.index} whatever string you want here');" />

12, - :

<whateverCustomTagDoes onClick="addBid('12 whatever string you want here') />

( JSP) , ( - JavaScript-eval-), :

<custom:tag onclick="addBid('<%=container_index%>string');" />
+4
onclick="addBid('<%out.print(container_index + "string");%>');"
0

el.

0

:

<c:set var="foo">
${var1 == true ? 'hello' : ''}
${var2 == true ? ' world' : ''}
</c:set>
0

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


All Articles