Skip to content Skip to sidebar Skip to footer

Struts Tags In Javascript

What would you consider a better practice? Would you do (in javascript): var aaa = ; or rather (in a jsp file):

Solution 1:

Well that depends I think. You can't use struts2 tags in JS files unless you save them as .jsp. In case you do this or you have a JS code in your page, I personally doesn't have a problem with the first option, since the tags will be processed back at server. I use this often when there is a simple assignment.
I don't like the second option cause it makes the code a little ugly and might cause some maintenance problems.
To sum it up it's a better practice to separate your JS and server side codes. If you find yourself mixing things up you might want to look at your design somewhere that could change. Again that depends on how you prefer to do it.


Solution 2:

You can achieve the desired results with both way.only thing matters is individual taste and preferences.

i would not like to mix up the things and will go with the second way using a hidden field.for me it serve 2 purposes.

  1. separating my struts2 tag from the JavaScript.
  2. More over i can write a simple clean JavaScript code with standard JavaScript method rather mixing up the things

Solution 3:

There are many ways, including what you've shown.

You may also:

  • Pass JavaScript files through the JSP processor
  • Use a different templating language for dynamic JS (like FreeMarker)
  • Emit a JSON object evaluated before your JS files are included (e.g., the contents of a hidden div are evaluated purely for JS side-effects)
  • Make an Ajax call for the same JSON object on page load before your functionality fires

Probably a few more. I don't prefer hidden fields unless they're in a form that isn't submitted (like a hidden, separate form) unless it's very clear which form values matter and which don't.


Post a Comment for "Struts Tags In Javascript"