在 JavaScript 函数中创建 Struts2 标签 [英] Create Struts2 tag inside JavaScript function

查看:28
本文介绍了在 JavaScript 函数中创建 Struts2 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在 JavaScript 函数中或使用 jQuery 创建 Struts2 标记(例如).

I would like to know if it is possible to create a Struts2 tag (e.g. ) inside a JavaScript function or using jQuery.

例如,我试过:

function createStrutsTag(){           

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

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

然而,它只是创建了包含标签 < 的 div.s:textfield label="HELLO"/> 没有被解析为它的真正含义.

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

我也尝试过使用 struts2-jquery-plugin 标签(sj)它也崩溃了......所以我想知道它是否可以完成,或者我应该在 HTML 部分创建这些元素正文,通过 CSS 样式隐藏,然后根据应用程序需要使用 JavaScript/jquery 函数显示或隐藏它们.

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.

推荐答案

JS 在客户端执行.JSP 标记在服务器上进行评估.

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

您有多种选择,不限于:

You have several options, not limited to:

使用文本标签的输出作为JS值

至少有两种方法可以做到这一点:

There are at least two ways to do this:

  • 将 JS 放入 JSP,或
  • 通过 JSP 处理器评估您的 JS

在JSP中评估一些JS并调用外部JS

Evaluate some JS in the JSP and call the external JS

例如,您可以在 JSP 中创建 I18N 标签等的哈希值,并将其传递给位于 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).

将文本标签的输出存储在一个隐藏的 DOM 元素中

使用 JS 检索它并像现在一样构建您的 DOM.

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

这可以说是最干净的.

使用 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.

这篇关于在 JavaScript 函数中创建 Struts2 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆