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
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