Skip to content Skip to sidebar Skip to footer

Create Struts2 Tag Inside JavaScript Function

I would like to know if it is possible to create a Struts2 tag (e.g. ) inside a JavaScript function or using jQuery. For instance, I tried: function createStrutsTag(){

Solution 1:

JS is executed on the client. JSP tags are evaluated on the server.

You have several options, not limited to:

Use the output of the text tag as a JS value

There are at least two ways to do this:

  • Put the JS in a JSP, or
  • Evaluate your JS via the JSP processor

Evaluate some JS in the JSP and call the external JS

For example, you could create a hash of I18N labels etc. in your JSP and pass it to JS functionality that lives in a JS file (where it should live).

Store the output of the text tag in a hidden DOM element

Retrieve it using JS and construct your DOM as you are now.

This is arguably the cleanest.


Rough overview of "clean" solution using jQuery

See this fiddle for the basics.

JSP:

<div id="hello" style="display: none;"><s:text label="HELLO" /></div>

JS:

function createDiv() {
  var $div = $("<div className='newer'>").html($("#hello").html());
  $("#outputHere").html($div);
}

It could be tightened up a bit, but that's the general idea.


Post a Comment for "Create Struts2 Tag Inside JavaScript Function"