Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring mvc addAttribute to model, how to get it from jsp javascript

i have a controller with a model which i do addAttribute("show", "yes");

how do I retrieve this value inside javascript?...assuming I have jstl

like image 215
john Avatar asked Mar 18 '11 17:03

john


1 Answers

Inserting it in a javasript would be the same as showing it in the html code of the jsp.

Try to do this:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
Show value is <c:out value="${show}"/>

if you can see the value in the JSP then JSTL is working. In any other case there may be another problem. For example that your configuration ignores EL. You can add this at the top of your JSP:

<%@ page isELIgnored="false" %>

When you see the value in the HTML code then the JSTL is working in that case you can use it in Javascript. As your setting the value for tha variable "show" to yes it cannot be used as a boolean value (because it should be true or false). In this case you should use it as a string adding quotations

<script type="text/javascript">
    var showVar = '<c:out value="${show}"/>';
    alert("The variable show is "+showVar);
</script> 

You can use Firebug to check that your javascript is working and you don't have any error on it.

like image 148
Javi Avatar answered Oct 05 '22 02:10

Javi