简体   繁体   English

jsp命令<%=%>在taglib标记语句内的Javascript语句中使用时会被忽略吗?

[英]jsp command <%=%> being ignored when it is used in a Javascript statement, inside a taglib tag statement?

An example will make this clearer! 一个例子将使这一点更加清楚!

The jsp file... jsp文件...

<%@ taglib prefix ="jam" uri= "http://jam.tld" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="ISO-8859-1"%>
<%
     String targetPage = true ? "toast" : "bread"; 
%>

<jam:text onmousedown="movePage('<%=targetPage%>');" id="<%=targetPage%>"><%=targetPage%></jam:text>

Note - the taglib is not mine and I have no control over it. 注意-taglib不是我的,我无法控制它。 (it isn't really called jam either :). (它也不是真正的果酱:)。

This then creates this HTML... 然后创建此HTML ...

<td onmousedown="movePage('<%=targetpage%>;');" id="toast">toast</td>

Which as you can see: the <%=targetPage%> was only replaced/parsed in the non-javascript bit? 如您所见:<%= targetPage%>仅在非JavaScript位中被替换/解析过?

The compiled jsp file looks like this: 编译后的jsp文件如下所示:

jspx_th_jam_005ftext_005f2.setOnmousedown("movepage('<%=targetpage%>')");

Anyone know what is going on, or how to fix it? 任何人都知道发生了什么事或如何解决? Why is the <%=%> tag being ignored when it is part of JavaScript statement? 为什么<%=%>标记作为JavaScript语句的一部分会被忽略? :) :)

Here's a bit of a cheat solution, you dont need the JSP tag in that JS call. 这里有一些作弊的解决方案,您在该JS调用中不需要JSP标记。 In fact, it's tidier this way 其实这样比较整齐

<jam:text onmousedown="movePage(this.id);" id="<%=targetPage%>"><%=targetPage%></jam:text>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM