aspose file tools*
The moose likes Struts and the fly likes Struts 2 - Using onclick in a radio tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2 - Using onclick in a radio tag" Watch "Struts 2 - Using onclick in a radio tag" New topic
Author

Struts 2 - Using onclick in a radio tag

Jim Borland
Greenhorn

Joined: Oct 26, 2009
Posts: 16
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!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Everything in the JSP is run on the server before being sent to the client (the browser)--that's what "JSP runs on the server, JavaScript runs on the client" means in a nutshell.

If you're neither (a) submitting a form, or (b) sending an Ajax request [, or (c) hitting a link with a GET param] there's no way for the server to know what you're doing on the client side. If your goal is to secretly persist the way the user is sorting (so it could be used the next time they hit that page, for example), the least intrusive way would be to just send it via an Ajax request. If the user has JavaScript turned off, you're screwed anyway the way it is now.
Jim Borland
Greenhorn

Joined: Oct 26, 2009
Posts: 16
David,

Thanks for the reply. I appreciate your explanation about JSP being run on the server before being sent to the client. I'm still trying to grasp the meaning and consequences of: "JSP runs on the server, JavaScript runs on the client."

There's nothing secret about my attempt to control the way the user is sorting. My JSP page is a list of records obtained from a database table, and the radio buttons at the top are various choices for ways the data can be sorted. I'd like for the user to click on one of the radio buttons and have the page immediately regenerated with all items in the requested sort sequence.

From what you said, I guess using an Ajax request is the only way I can do this. I know nothing about Ajax, so I guess this is a good time to start learning.

Thanks again for your reply.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No, you could submit a form via JavaScript when the radiobox changes.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Jim Borland wrote:I'm still trying to grasp the meaning and consequences of: "JSP runs on the server, JavaScript runs on the client."

Perhaps this article can help.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jim Borland
Greenhorn

Joined: Oct 26, 2009
Posts: 16
What a fantastic article! Thanks for the reference. Now I think I understand. All of the Java code and JSP tags are used on the server to generate the plain HTML code which gets sent to the client. Since the JavaScript function runs in the client's browser, the only thing that can be in it is stuff that HTML recognizes (no Java code or JSP tags).
Jim Borland
Greenhorn

Joined: Oct 26, 2009
Posts: 16
In a previous post, David said:

> No, you could submit a form via JavaScript when the radiobox changes.

I did a Google search on "submit a form via JavaScript," found a couple of good web pages, made a visit to: http://www.w3schools.com/js/js_intro.asp to read their post on "HTML DOM Form Object," and obtained a sample code snippet there for using the form submit() method. I adapted that code example to my program and it works perfectly! When I click on a radio button, the page immediately regenerates with the table in a different sort order. Many thanks to you, David, for pointing me in this direction! I never would have figured this out without your help.
Tudor Raneti
Ranch Hand

Joined: Nov 29, 2009
Posts: 145
Check out my answer on this javascript JSP interaction problem, maybe you find it inspiring:
http://www.coderanch.com/t/474023/JSP/java/use-Javascript-variable-value-JSP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts 2 - Using onclick in a radio tag
 
Similar Threads
can i pass a variable through javascript function to another struts jsp page?
Execute the Code
Value from select element is null in request
pass value from struts2 form to javascript
Need to pass Javascript Array to Struts 2 Action