How to access Struts 2 options in jQuery

I am trying to access the Struts 2 property through jQuery.

My Common.js file looks like this:

$(document).ready(function() { alert("<s:property value='myVariable'/>"); }); 

When I load my view, it warns the entire line, rather than resolving the Struts 2 variable. I assume that there are only syntactic tricks that I miss ... any help would be appreciated. Thanks!

+4
source share
1 answer

You must put the script in JSP

 <head> <script type="text/JavaScript"> $(document).ready(function() { alert(<s:property value="myVariable"/>); }); </script> </head> 

you cannot use struts or other JSP tags inside js files. It is not compiled by the server. However, you can use a function in JSP that calls these scripts by passing parameters to it.

 <head> <script type="text/javascript" src="<s:url value='/js/Common.js'/>"></script> <script type="text/JavaScript"> $(document).ready(function() { alertMyVariable(<s:property value="myVariable"/>); }); </script> </head> 

In js:

 function alertMyVariable(myVariable) { alert(myVariable); } 
+3
source

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


All Articles