0

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(){           

        var newElement;
        newElement= document.createElement("div");
        newElement.className = 'newer';
        newElement.innerHTML = '<s\:textfield label="HELLO" \/>';              

        newElement.css('visibility','visible'); }

However, it just created the div containing the label < s:textfield label="HELLO" /> without being parsed to its real meaning.

I also tried with struts2-jquery-plugin tags (sj) which also crashed...so I would like to know if it can be done or I should create those elements in the HTML part of the body, being hidden by CSS style and then show or hide them using JavaScript /jquery functions as they are needed by the application.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

For the moment, I just included the JS in a JSP. I made it work by creating the text tag inside the JSP with visibility:hidden. Then, inside the JS function I just turn its visibility to visible. However, this is not really the answer to my question :S Please, could you provide a simple example of the cleanest method?? Thanks.
Could be possible to edit the content of the struts tag depending of any other variable from the JS domain? or just I can do this with fixed content? Thanks for your quick answers and fiddle.
@aloplop85 I'm not sure what you mean by "edit the content of the Struts tag". Again: tags are evaluated on the server before the client has seen the response, thus before the client could do anything affecting the tag itself via JS.
+1... @aloplop85 the clearer is the question, the better are the answers... you can do anything with Struts2, even creating your own components, but nothing will ever let you handle them from JS (client) once the page is processed (on the server).
Also, if you knew how the tags are processed by the server you could change any of its properties, couldn´t you? (e.g. $($div[0].children[0]).css("color","red"); )
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.