Using
Struts 2, I'm trying to set up a
JSP file that has 3 radio buttons at the top of the page, and when the user selects one of them (without needing to hit a 'submit' button), an action will be called and a new page loaded into the browser. The content of the new page will depend on which of the three radio buttons is selected by the user.
In the JSP page I have these tags:
When I click one of the radio buttons, it calls a Javascript function named newSortOrder, defined in the <head> portion of that JSP file:
(1) document.write(value); – writes the value of "value" to the screen, and I can see that this aspect is working correctly (it shows the proper value for each button).
(2) the href property works great and executes the action which calls another JSP file. In this
test case it is a generic JSP file that does not depend on "value."
I'm stuck on trying to figure out how to send the value of "value" to the action so it can affect which JSP page is loaded next. I tried saving "value" to the session to be retrieved by the action, with this line added to my "newSortOrder" Javascript function:
but it didn't work. Error message: "value cannot be resolved."
After doing a bunch of web searches on things like: "javascript function parameters in
java scriptlet" I've learned my problem is that Javascript runs on the client but JSP runs on the server, and they cannot directly communicate with each other. Still, it seems like it should be possible for the Java "session.setAttribute" command to wait until Javascript runs, and at that point fill in a value for its variable "value."
The suggestions I've seen for making this work involve providing a hidden input field in your form and writing the value of the variable to this input field before you submit your form. My problem is that I'm not submitting a form in this case, just using the onclick attribute.
Any ideas for how I can pass this value to my action class? Thank you!