0

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>
11
  • Is this code in a .jsp file, by chance? Commented May 19, 2013 at 6:13
  • @ajp15243 Yep. JSP EL Expressions. Commented May 19, 2013 at 6:35
  • Dear @user2378250, what do you mean by print the value of a Spring variable in the browser?? Alert it? Print in the console? Put it in another HTML element? Commented May 19, 2013 at 6:37
  • What are you trying to print? Which variable needs to be printed? Commented May 19, 2013 at 6:45
  • Your JSP file is evaluated server-side, meaning it is not executing the JavaScript code at all, it's just going through and (in this case) just replacing ${...} with the string value of that given expression (which all seem to just contain variable names). Whatever values pop out there are what end up being sent as JavaScript to the client (the browser), where it is then executed as JS. You should be able to do something like alert("${myVariable}");, which gets sent as alert("some-value"); to the client, and is then executed as an alert with the value that the server placed there. Commented May 19, 2013 at 7:00

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

thanks for your reply...how to print this pring message value in the browser and see <spring:message code='BH-MS-0070'/>
@user2378250 You can put <spring:message .../> tags in where you put ${...} in a console.log or alert statement. It works the same way, in that the JSP Spring tag is evaluated first and a value is put into the JavaScript code, and then that rendered JavaScript code is sent to the browser for execution. You can even just put <spring:message .../> in your HTML (like <span> <spring:message .../> </span>), and its value will just be printed to the final HTML page in the browser.
thanks for your reply when i put alert for the spring message it shows error
You're going to need to give more detail than that. It is probably appropriate to open a new question about it, detailing what you wrote and the error you are getting.
alert("i am spring" +"${<spring:message code='BH-MS-0070'/>}");
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.