This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Cloud Application Architecture Patterns: Designing, Building, and Modernizing for the Cloud and have Kyle Brown, Bobby Woolf and Joseph Yodor on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Javascript with struts and Java

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all I have a struts application that I want to use some javascript in. Here is my situation: I have a struts form that is asking a user for some address information. In that form there is a drop down listing a few often used addresses. If the user selects one of these values then the form should reload with all of the values of that default. (the values can be pulled from a database call).
My problem the following:
1) How do you reference a form in struts if there is no name for the form ?
I have been trying things like: document.form.shipToName.value = test;
and have not had any success.
2) How do I get my javascript to pull out the values from the java database call ?


thanks for any help or suggestions you might have.
 
Sheriff
Posts: 67756
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

Originally posted by A knibbs:
1) How do you reference a form in struts if there is no name for the form ?



It's been many years since I abandoned Struts, but does the Struts form tag not allow you to specify the name attribute? I find that hard to believe.

But if that does turn out to be the case, you can always reference your form via the forms array:

document.forms[0]

This is not the best way to do this, a name is better, as it is fragile and can easily get broken if more forms are added to the page.

2) How do I get my javascript to pull out the values from the java database call ?

JavaScript has no means to access the database. You'll either need to submit the page and repaint it with the updated information, or use Ajax to fetch the data from the server. Modern web apps opt for the latter.
[ August 28, 2007: Message edited by: Bear Bibeault ]
 
A knibbs
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:


It's been many years since I abandoned Struts, but does the Struts form tag not allow you to specify the name attribute? I find that hard to believe.



I am going based on the information that I found here:
http://struts.apache.org/1.2.8/userGuide/struts-html.html#form

It is quite possible that I am misunderstanding something.


Originally posted by Bear Bibeault:

But if that does turn out to be the case, you can always reference your form via the forms array:

document.forms[0]

This is not the best way to do this, a name is better, as it is fragile and can easily get broken if more forms are added to the page.

2) How do I get my javascript to pull out the values from the java database call ?

JavaScript has no means to access the database. You'll either need to submit the page and repaint it with the updated information, or use Ajax to fetch the data from the server. Modern web apps opt for the latter.

[ August 28, 2007: Message edited by: Bear Bibeault ]



I am aware that javascript has no access to the database, but thank you for giving me all the options that I would have in regards to loading the information. I was hoping that I would be able to extract the information from a java bean that would hold the information, or possibly store it in the session seeing as how little information there is I thought that would be better than reloading the page.
[ August 28, 2007: Message edited by: A knibbs ]
 
Bear Bibeault
Sheriff
Posts: 67756
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

Originally posted by A knibbs:
I was hoping that I would be able to extract the information from a java bean that would hold the information, or possibly store it in the session seeing as how little information there is I thought that would be better than reloading the page.


But how would the JavaScript have access to these server-side constructs?

You've got to make a request back to the server; which means either a submit or Ajax.
 
Bear Bibeault
Sheriff
Posts: 67756
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
Or...

If the data is of a reasonable size, as you indicate, you can preload the data into JavaScript constructs that you access on the page.
 
A knibbs
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most it would be is a dozen addresses. Does this seem like too much information to store in javascript concepts ?

Thanks for all the help so far Bear.
 
Bear Bibeault
Sheriff
Posts: 67756
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

Originally posted by A knibbs:
The most it would be is a dozen addresses. Does this seem like too much information to store in javascript concepts ?



That's not a whole lot of data. I'd not see any reason not to simply store it in JavaScript data when the page loads. That way, it will available to your event handlers.

Addressess seem safe enough, but be aware that this technique should not be used for sensitive data as anyone can view the source of your page to see it.
 
A knibbs
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the warning, but the way I look at it is as follows. The only manner in which to get to this page is to have logged in and be an authorized user, so weather they would see it in the source or in the app itself is pretty much the same result. (Nice to see that someone has concern over privacy of data and ensuring that only those who require information are given it. Seems like far to many people are selling their information short for no particular reason).
 
Bear Bibeault
Sheriff
Posts: 67756
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

Originally posted by A knibbs:
The only manner in which to get to this page is to have logged in and be an authorized user, so weather they would see it in the source or in the app itself is pretty much the same result.



That's good. I was more thinking of a situation where someone might try to "save time" by uploadig multiple client's information to the page or something similar.
 
A knibbs
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the wisdom you have shared Bear. I appreciate it.
reply
    Bookmark Topic Watch Topic
  • New Topic