struts2+spring3:将变量从action传递到jsp(javascript代码)

发布于 2024-11-05 19:48:28 字数 822 浏览 1 评论 0原文

首先,非常感谢本论坛中所有试图解决我们问题的人;)

我正在使用struts2+spring3开发一个Web应用程序。我有一个关于如何将变量(数组的数组)从动作类传递到jsp页面的问题。

我知道您唯一需要做的就是在我的操作中声明一个私有变量以及该变量的 get 方法,然后就可以从 jsp 访问这些变量,我已经做到了并且它有效。

public class Workspace extends ActionSupport {

private String[][] objects=null;

public String[][] getObjects() {
    return objects;
}

public String execute() throws Exception{

问题是我想在加载整个网页之前从 javascript 代码访问这个变量。

我尝试过不同的方法,但从来没有奏效。

$(function() {
var objectsMap=new Array();

    $(document).ready(function() {
          objectsMap = $objects;
    });

两者都不起作用(警报说:“值:未定义”):

    <s:set name="auxObj" value="%{objects}"/>
    <script>
        alert("Value: "+$("#auxObj").val());
    </script>

有人知道我该怎么做吗?

First of all, thank you very much to all these that are trying to solve our problems in this forum;)

I am developing a web application with struts2+spring3. And I have a question about how to pass a variable (an array of arrays) from the action class to the jsp page.

I know that the only you need to do is to declare a private variable in my action and a get method for that variable and then its possible to access these variable from the jsp, I have done it and it works.

public class Workspace extends ActionSupport {

private String[][] objects=null;

public String[][] getObjects() {
    return objects;
}

public String execute() throws Exception{

The problem is that I want to access to this variable from the javascript code before loading the whole web page.

I have tried by different ways but it is never working.

$(function() {
var objectsMap=new Array();

    $(document).ready(function() {
          objectsMap = $objects;
    });

neither works (the alert says: "Value: undefined"):

    <s:set name="auxObj" value="%{objects}"/>
    <script>
        alert("Value: "+$("#auxObj").val());
    </script>

Anyone have idea about how could I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

べ映画 2024-11-12 19:48:28

对于 Web 开发初学者来说,了解 JSP 等服务器端语言在 Web 服务器上运行,而 JS 等客户端语言在 Web 浏览器上运行非常重要。 JS 与 JSP 不同步运行。 JSP 只生成 HTML/CSS/JS 代码。网络服务器将其发送到网络浏览器。 JS 只能看到/理解 HTML。

每当你想将 JSP 变量分配给 JS 时,你需要让 JSP 打印它就像它是 JS 代码。下面是一个使用 JSTL 标记来迭代 Object[][] 的示例(我相信 Struts 也有自己的迭代器标记,< s:iterator>,但由于我不使用 Struts,所以我不确定如何使用它以及 JSTL 是否应该工作得很好)。

<script>
    var objects = [];

    <c:forEach items="${objects}" var="row" varStatus="x">
        objects[${x.index}] = [];
        <c:forEach items="${row}" var="column" varStatus="y">
            objects[${x.index}][${y.index}] = '${column}';
        </c:forEach>
    </c:forEach>
</script>

这样,当 JSP 完成生成 HTML/JS 代码的任务时,假设您有一个包含 2 行和 2 列的 Object[][] (您可以验证它你自己在网络浏览器中打开页面,右键单击它并选择查看源代码):

<script>
    var objects = [];
    objects[0] = [];
    objects[0][0] = 'row1col1';
    objects[0][1] = 'row1col2';
    objects[1] = [];
    objects[1][0] = 'row2col1';
    objects[1][1] = 'row2col2';
</script>

这样JS代码可以通过对象访问它。

For web development starters, it's important to understand that server side languages like JSP runs on webserver and client side languages like JS runs on webbrowser. JS does not run in sync with JSP. JSP only produces HTML/CSS/JS code. Webserver sends it to webbrowser. JS only sees/understands HTML.

Whenever you want to assign JSP variables to JS, you need to let JSP print it as if it is JS code. Here's an example with JSTL <c:forEach> tag to iterate over Object[][] (I believe Struts has also its own iterator tag, <s:iterator>, but since I don't use Struts, I'm not sure how to use it and if it is applicable; JSTL should work as good).

<script>
    var objects = [];

    <c:forEach items="${objects}" var="row" varStatus="x">
        objects[${x.index}] = [];
        <c:forEach items="${row}" var="column" varStatus="y">
            objects[${x.index}][${y.index}] = '${column}';
        </c:forEach>
    </c:forEach>
</script>

This way it'll end up as follows when JSP has done its task of producing HTML/JS code, assuming that you have an Object[][] with 2 rows and 2 cols (you can verify it yourself by opening page in webbrowser, rightclicking it and choosing View Source):

<script>
    var objects = [];
    objects[0] = [];
    objects[0][0] = 'row1col1';
    objects[0][1] = 'row1col2';
    objects[1] = [];
    objects[1][0] = 'row2col1';
    objects[1][1] = 'row2col2';
</script>

This way JS code can access it by objects.

jJeQQOZ5 2024-11-12 19:48:28

谢谢你们,
它终于可以工作了,我在这里输入代码,以防万一对其他代码有用:

    var $linesMap=new Array();

    $(document).ready(function() {
        var $arr;
        <s:iterator value="objects" var="item" status="stat">
            $arr=new Array();
            <s:iterator value="item" var="item2" status="stat2">
                $arr.push(['${item2[0]}','${item2[1]}']);
            </s:iterator>
            $linesMap.push($arr);
        </s:iterator>
        });

然后我可以在 javascript 代码中使用 $linesMap :)

非常感谢,
阿莱克斯

Thank you people,
It works finally, I type the code here for in case is useful for other one:

    var $linesMap=new Array();

    $(document).ready(function() {
        var $arr;
        <s:iterator value="objects" var="item" status="stat">
            $arr=new Array();
            <s:iterator value="item" var="item2" status="stat2">
                $arr.push(['${item2[0]}','${item2[1]}']);
            </s:iterator>
            $linesMap.push($arr);
        </s:iterator>
        });

Then I can use $linesMap in the javascript code :)

Thank you very much,
Aleix

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文