• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Struts 2 - Using onclick in a radio tag

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you could submit a form via JavaScript when the radiobox changes.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Jim Borland
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out my answer on this javascript JSP interaction problem, maybe you find it inspiring:
https://coderanch.com/t/474023/JSP/java/use-Javascript-variable-value-JSP
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic