24fos
24fos

Reputation: 89

Need to access Java class variable from javascript

I have a server-side java application utilizing struts/jsp. For simplicity I won't go into the details of it, and because I'm only concerned about the value of a particular class variable after my application has been executed. This particular class is directly executed from struts.

My application is executed after I submit a form from my JSP page. When the action completes, the value of one of my class variables is updated, and I want to access the value of that class variable from a javascript function in my jsp.

Currently I can easily display the value of any class variable in the jsp file by referring to the action name that I defined in struts, and the class variable name. Purely as an example, here's what I would have in my jsp:

<s: property value="myInformation.birthDate" />

And in the class, birthdate would look something like this:

private String birthDate;

Of course the getters/setters:

public String getBirthDate(){
     return birthDate;
}

public void setBirthDate(String birthDate){
     this.birthDate = birthDate;
}

Here's what struts would look like:

<action name="myInformation" 
        class="com.whatever.whatever2.MyInformation" method="execute">
        <result name="success"></result>
        <result name="error"></result>
</action>

What I want to do is store this value in a javascript variable after my application has been executed, for the purpose of loading the value into a javascript array.

I just need help capturing the value of "myInformation.birthDate" and storing it in some javascript variable.

EDIT - Adding details from the comments for further clarification:

The function is in a file.js which I included in my JSP. The JSP contains a jqgrid. The function uses JQuery's "saverow" to make jqgrid columns editable and pass them into the java application. The function loops for each row that is selected. After each execution, "birthDate" has a new value" I want to load each value into an array within the function.

Upvotes: 2

Views: 5416

Answers (1)

Guy Gavriely
Guy Gavriely

Reputation: 11396

3 options sorted (arguably) from worst to better:

  1. render the javascript as part of the page as @DaveNewton commented
  2. have the value hidden in the html document and get it with javascript getElementBy... or jquery like
  3. make an ajax call and return a proper JSON

Upvotes: 3

Related Questions