Struts 1, Get Function return in Java Script

234 views Asked by At

I have a function called checkSubOffer in my Action class, which will return a Boolean value. I want this value in my JavaScript function docheckSubOf.

 function docheckSubOf(){
    thisForm.method.value = "checkSubOf";       
}

is there a way to do it. I am using Struts 1

1

There are 1 answers

0
Dave Newton On

Generally speaking, you don't, at least not directly.

There are (at least) four options:

Option 1: Render your JavaScript in the JSP file, use normal S1 JSP methods to inject it into the JS. It will need to be properly JavaScript-escaped.

Option 2: Render your JavaScript through the JSP processor, e.g., add *.js to the list of files that need JSP processing. IMO not preferred, but it's an option. Other frameworks do this (like Rails ERb JS templates) but it's a little counter-inuitive, and can make finding functionality incrementally more difficult.

Option 3: Retrieve the value later via Ajax. Likely also not preferred since there's a mechanism you can use to avoid it. If there's a lot of other Ajax on the page it might not be as bad, but requires a new or modified JSON endpoint etc.

Option 4: Render the data into the JSP using normal JSP methods. The JS, which in general should be externalized to a .js file anyway, can refer to this data only after the page is fully reader, e.g., $(function () { ...data is ready... }.

You'd probably want to namespace/modularize the data as well, but that's a general good practice anyway.