How to pass a Javascript variable to <jsp: setProperty> and JSTL?

How to pass a Javascript variable to JSTL?

<script>

    var name = "john";  

    <jsp:setProperty name="emp" property="firstName" value=" "/>   // How do I set javascript variable(name) value here ?

    <c:set var="firstName" value=""/>  // How do I set javascript variable (name) value here ?     

  </script>
+3
source share
3 answers

You need to send it as a request parameter. One way is to fill in a hidden input field.

<script>document.getElementById('firstName').value = 'john';</script>
<input type="hidden" id="firstName" name="firstName">

This way you can get it on the server side as a request parameter when the form was submitted.

<jsp:setProperty name="emp" property="firstName" value="${param.firstName}" />

An alternative way is to use Ajax, but this is a completely new story / answer.

See also:

, !

+5

AFAIK JavaScript JSTL . JSTL , <jsp:> HTML. , <jsp:> , ; HTML/. <jsp:> JavaScript, .

: , <jsp:> .

+2
<script>
var name = "<jsp:getProperty name="emp" property="firstName" />";
</script>

JSP code is executed before JavaScript, so by the time the JavaScript is processed, the tag will be replaced by the contents of emp.firstName.

0
source

Source: https://habr.com/ru/post/1760365/


All Articles