I'm having a trouble with using jsp variables as javascript parameters.
The javascript function:
function user(name, lastname, nick) {
return name + " " + lastname + " (" + nick + ")";
}
And using it in 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>
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 :)
//EDIT
ok, I'll try to clarify it a little more:
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 - is the user entity, I wanted to have it nicely written in the table in pattern -
Name Lastname (userName)
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?
JasperException? JavaScript errors dont show up in tomcat and having invalid or JavaScript that doesnt work isnt something that would show up in the Tomcat logs. A JasperException means that your have a JSP error.