user2378250
user2378250

Reputation: 23

Print a Spring variable in browser using JavaScript

I am trying to print the variable in the if condition. It's done using Spring and JavaScript. I don't know Spring. How do I print the value of a Spring variable in the browser?

   <!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("p").click(function(){
    $(this).hide();
  });
});
</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>
</body>
</html>

Upvotes: 0

Views: 1901

Answers (1)

ajp15243
ajp15243

Reputation: 7952

I'll be attempting to piece together my comments on your question into an answer, since they seem to have solved it.

What you should first understand is the difference between server-side code parsing and execution (a JSP page for you), and client-side code parsing and execution (HTML and JavaScript for you). You can read a brief overview in the top-rated answers to this programmers.stackexchange.com question. Further reading can be found on Wikipedia's articles on server-side scripting and client-side scripting. A key concept to take away from the client-side scripting article about server-side scripts is:

[Server-side scripts] produce output in a format understandable by web browsers (usually HTML), which is then sent to the user's computer. The user cannot see the script's source code (unless the author publishes the code separately), and may not even be aware that a script was executed. Documents produced by server-side scripts may, in turn, contain client-side scripts.

In terms of your setup, this means that when you request your JSP page via your browser, your browser tells the server that is hosting the JSP page that the browser wants that page. The server then executes the JSP page to produce a document that is purely HTML and JavaScript in nature (perhaps some CSS too, if you wrote it that way) for sending to the browser. The browser never sees the JSP code. By the time the server is done parsing and executing the JSP page, all instances of ${someExpression} (JSP EL statements) and other JSP-specific expressions (such as taglibs, like <spring:xx> tags) will be resolved to some client-side equivalent. For basic JSP EL statments (${...}), these resolve to some string value. If you happen to cleverly place that EL statement in some JavaScript in the JSP, such as console.log("${myVariable}");, then you are dynamically producing JavaScript code that will depend on the output of the EL statement (in this example, it would resolve to something like console.log("myVariable-value");).

Up to this point, the server has just been executing JSP code (and otherwise leaving HTML/JS/CSS already in the page intact). No JavaScript has been parsed or executed. This final "rendered" page, after the server is done executing JSP stuff, is then sent as purely HTML/JS/CSS to the client browser, which receives it and parses/executes it. At this point, the browser sees something like console.log("myVariable-value");, without knowing or caring that your JSP code produced it, and executes that (since you cleverly placed your EL statement to produce valid JS).

This long-winded explanation then means you can do the following:


Server-side JSP:

alert("${createBehavior.behaviorRevisionAllowed}");

Resulting code sent from the server to the client browser:

alert("false");

Result after code is executed in the browser: An alert box pops up with the value false in it.


Server-side JSP:

console.log("${createBehavior.behaviorRevisionAllowed}");

Resulting code sent from the server to the client browser:

console.log("false");

Result after code is executed in the browser: The value false is logged to the console.


Server-side JSP

return [$('#some-id'), "<spring:message code='BH-MS-0070'/>"];

Resulting code sent from the server to the client browser:

return [$('#some-id'), "some-spring-message-string"];

Result after code is executed in the browser: The array in the JavaScript return statement is created with the value "some-spring-message-string" as the second element.


From your question in your comment about an "EL variable", when I said "EL variable" I meant the fact that an EL statement, such as ${someStatement}, could be a statement as simple as a variable name, such as ${createBehavior.behaviorRevisionAllowed}. This EL statement will see just a variable name in it, attempt to find that variable and get its value, and then replace ${createBehavior.behaviorRevisionAllowed} with the actual value.

Let me know if further explanation is needed.

Upvotes: 1

Related Questions