2

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.

5
  • 2
    <script>var foo = "${myInformation.birthDate}"; or whatever. What's the issue? Commented Jan 15, 2014 at 2:08
  • when I use an alert to display the value: alert(foo), I see "${myInformation.birthDate}" as a string literal on the alert popup, and not the actual value. Commented Jan 15, 2014 at 2:43
  • Is it in a JSP page? Are you in a reasonable servlet container? You need to provide more info. In any case, you can hide it in the DOM as mentioned, render only js data in the JSP and call out to the js functionality, etc. Commented Jan 15, 2014 at 2:47
  • 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. Commented Jan 15, 2014 at 2:59
  • @roostermacallan Why not to take it directly from the row? Should it be easy to map those values to the rowid and save between requests? Commented Jan 15, 2014 at 9:12

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

4 Comments

option 1 not working. more details above. I'll try #2 next. Thanks.
Make this: <script>var foo = "${myInformation.birthDate}"; as inline javascript for #1 to work rather than keeping it in a separte js file.
I ended up using the second half of Guy's 3rd answer. I used the "aftersavefunc" parameter of the jqgrid saverow function to parse the response as JSON, and then extract the specific value that I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.