简体   繁体   English

在JavaScript函数中创建Struts2标记

[英]Create Struts2 tag inside JavaScript function

I would like to know if it is possible to create a Struts2 tag (eg ) inside a JavaScript function or using jQuery. 我想知道是否可以在JavaScript函数内部或使用jQuery创建Struts2标记(例如)。

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. 但是,它只是创建了包含标签<s:textfield label =“HELLO”/>的div而没有被解析为它的真正含义。

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. 我也试过struts2-jquery-plugin标签 (sj)也崩溃了...所以我想知道它是否可以完成或者我应该在身体的HTML部分创建这些元素,被CSS样式隐藏然后使用JavaScript / jquery函数显示或隐藏它们,因为应用程序需要它们。

JS is executed on the client. JS在客户端上执行。 JSP tags are evaluated on the server. JSP标记在服务器上进行评估。

You have several options, not limited to: 您有多种选择,不仅限于:

Use the output of the text tag as a JS value 使用text标记的输出作为JS值

There are at least two ways to do this: 至少有两种方法可以做到这一点:

  • Put the JS in a JSP, or 把JS放在JSP中,或者
  • Evaluate your JS via the JSP processor 通过JSP处理器评估您的JS

Evaluate some JS in the JSP and call the external JS 在JSP中评估一些 JS并调用外部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). 例如,您可以在JSP中创建I18N标签等的哈希值,并将其传递给JS文件中的JS功能(它应该存在的位置)。

Store the output of the text tag in a hidden DOM element 将文本标记的输出存储在隐藏的DOM元素中

Retrieve it using JS and construct your DOM as you are now. 使用JS检索它并像现在一样构建DOM。

This is arguably the cleanest. 这可以说是最干净的。


Rough overview of "clean" solution using jQuery 使用jQuery粗略概述“干净”解决方案

See this fiddle for the basics. 看看这个小提琴的基础知识。

JSP: JSP:

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

JS: 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. 它可能会收紧一点,但这是一般的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM