开发者

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

开发者 https://www.devze.com 2023-02-18 12:54 出处:网络
i have a controller with a model which i do addAttribute(\"show\", \"yes\"); how do开发者_JS百科 I retrieve this value inside javascript?...assuming I have jstlInserting it in a javasript would be th

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

how do开发者_JS百科 I retrieve this value inside javascript?...assuming I have jstl


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号