jsp变量作为javascript函数参数 [英] jsp variable as javascript function parameters
问题描述
使用jsp变量作为javascript参数时遇到麻烦.
I'm having a trouble with using jsp variables as javascript parameters.
javascript函数:
The javascript function:
function user(name, lastname, nick) {
return name + " " + lastname + " (" + nick + ")";
}
并在jsp中使用它:
<tbody>
<c:forEach var="et" items="${eTestsToAdd}">
<tr>
<td><script>document.write(user(${et.author.name}, ${et.author.lastname}, ${et.author.nick}));</script></td>
也可以在另一个示例中使用
Also it works on another example:
js
function parseToDate(dateInMiliseconds) {
var d = new Date(dateInMiliseconds);
var string = d.getDay().toString() + "-" + d.getMonth().toString() + "-" + d.getFullYear().toString();
return string;
}
jsp
<script>document.write(parseToTime(${uc.startDate.time}));</script>
有两个区别,工作示例是带有一个参数的javascript函数且参数为int,而无效的例子是带有三个字符串参数的javasript函数.如何传递这些值以使其起作用? 请不要编写脚本:)
There are two differences, working example is javascript function with one parameter and the parameter is int, and not working one is javasript function with three string parameters. How can I pass those values to have it working? No scriptlets please :)
//编辑
好吧,我会尝试澄清一下:
ok, I'll try to clarify it a little more:
我在jsp中有一个要显示一些数据的表:
I have a table in jsp where some data is to be displayed:
<tbody>
<c:set var="i" value="0" />
<c:forEach var="uc" items="${userClasses}">
<c:set var="i" value="${i+1}" />
<c:url var="usrURL" value="/users/show/${uc.user.nick}" />
<tr onclick="location.href = '${usrURL}' ">
<td>${i}</td>
<td><img class="img-circle img-little" src="<c:url value='/imageView/${uc.user.avatar.id}'/>" />
<script>document.write(user(${uc.user.name}, ${uc.user.lastname}, ${uc.user.nick}));</script>
</td>
<td><script>document.write(parseToTime(${uc.startDate.time}));</script></td>
</tr>
</c:forEach>
</tbody>
uc.user-是用户实体,我想将它很好地写在表中的模式中-
uc.user - is the user entity, I wanted to have it nicely written in the table in pattern -
姓氏(用户名)
使用我在此处发布的JavaScript. 但是,当我在jsp中使用此函数时,tomcat将我抛出org.apache.jasper.JasperException:在我调用js函数的那一行处理JSP页面时发生了异常.所以很明显,我在jsp中以某种方式使用了错误的代码;我很喜欢javascript.我的问题是如何在此处正确使用此javasript函数?
with the javascript I posted here. But when I use this function in jsp, tomcat throws me org.apache.jasper.JasperException: An exception occurred processing JSP page at the line where I call the js function. So obviously, I'm using it wrong in jsp somehow; I'm fresh with javascripts, though. And my question is how to use this javasript function properly here?
推荐答案
我不确定这是不知道eTestsToAdd
集合的值是什么的解决方案,但是肯定是 one 问题.
I am not certain that this is the solution without knowing what the values for the eTestsToAdd
collection is, however this would certainly be one problem.
给出以下代码片段:
document.write(user(${et.author.name}, ${et.author.lastname}, ${et.author.nick}));
作者的值分别为joe
,shmoe
,js
,这将导致输出
And the values for the author are joe
, shmoe
, js
respectively this would result in this output
document.write(user(joe, shmoe, js));
这是无效的JavaScript,JS评估程序将查找名为joe,schome和js的变量.您需要将输出用引号引起来.
This is invalid javascript, the JS evaluator would look for variables named joe, schome and js. You need to wrap the output in quotes.
document.write(user("${et.author.name}", "${et.author.lastname}", "${et.author.nick}"));
现在,如果有人在名字中输入名称lovemesome"XXS
,您还将收到一个JavaScript错误.您将需要清理输出变量,在这种情况下,可以使用以下命令进行处理:
Now if someone puts in the name lovemesome"XXS
for the first name you will also get a javascript error. You will need to sanitize your output variables, you can do that for this case by using the following:
${fn:replace(${et.author.name}, '\"', '\\\"'}
这篇关于jsp变量作为javascript函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!