-1

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

user6751235
  • 59
  • 2
  • 12
  • Please, provide us the complete code.. – reisdev Jun 06 '18 at 11:20
  • You are asking about `javascript`, yet you are saying that you are using `apache struts` which is `java`. Which technology are you using in the end? – Jaroslaw Pawlak Jun 06 '18 at 11:28
  • @JaroslawPawlak Like most everybody, likely both, as indicated in the question. – Dave Newton Jun 06 '18 at 13:59
  • My JSP page has HTML and JavaScript. its using Structs to do the Login. So when Login Button is clicked it's redirected to the corresponding action page. Based on the return value of the function checkSubOf (which is written in Java) I will decide whether to trigger the action. Currently im able to do the login successfully, but im not sure inside – user6751235 Jun 06 '18 at 15:13
  • You don't; JS runs only on the client. I gave you the options. Although if it's something that's running on the server side before the page is rendered you should be handling it in your action and deciding what to do there. – Dave Newton Jun 06 '18 at 18:12

1 Answers1

0

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.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302