2

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?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Aleix
  • 663
  • 1
  • 8
  • 22

2 Answers2

1

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you BalusC for the useful information about where runs the two kind of languages. – Aleix May 07 '11 at 17:28
  • +1 for a great piece on server vs. client. @Aleix: Change `${objects}` to `${action.objects}` in BalusC's example and it should work with your action. – Steven Benitez May 07 '11 at 19:43
  • Thank you Steven but it finally worked ok referencing "objects" directly, it was not necessary to refer to "action.objects". Thanks;) – Aleix May 18 '11 at 23:17
1

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

Aleix
  • 663
  • 1
  • 8
  • 22