This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes Can javascript function set value of a form bean property? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Can javascript function set value of a form bean property?" Watch "Can javascript function set value of a form bean property?" New topic
Author

Can javascript function set value of a form bean property?

Sudha Joish
Greenhorn

Joined: Nov 07, 2003
Posts: 12
My JSP presents a bunch of text boxes. When user submits the JSP, I need to generate a date/time string and insert the info frm the text boxes + this date string as a new record in my DB.
I have a form bean that has getter and setter methods for text box elements and date_time ( -- a hidden field on the JSP )

JSP Submit:

Javascript:


In regular JSP/Javascript, I would have done:

But here since the form bean is associated with the form elements, when I want to set the value for a form bean property, what shd I do? Is this even possible frm a Javascript function??
Please help.
Thanks
-- $uDhA
Tzanko Stefanov
Greenhorn

Joined: Nov 14, 2003
Posts: 4
Setting the form bean property is not done on the server side when the request is posted. The form bean properties will be set even if you use plain html inputs with names corresponding to the bean property.
We change the values of the inputs via JS all the time. <html:text ...> will generate an input tag with the name specified. Then you just set the value of that element and submit the form! Do not forget to submit the form in you JS when using an event like onSubmit="yourFunction()"
sandy gupta
Ranch Hand

Joined: Jan 30, 2001
Posts: 228
Sudha,
The right way to do it would be to add the date time stamp in your service layer if you are using the jsp->action->service->dao layers or atleast in the action layer. Avoid doing this in the jsp if you can. Your peers will not be expecting to look for business logic in the jsps if someone else wants to maintain your app, the will look for the service layer or at least the action layer.
Thanks
Sahil


Adios
Surasak Leenapongpanit
Ranch Hand

Joined: May 10, 2002
Posts: 341
In your form class, you can initialize a current datetime on the date_time.
date_time = ....;
public String getDate_time() {
return this.date_time;
}
You don't want setDate_time function if you don't want to set it.
Sudha Joish
Greenhorn

Joined: Nov 07, 2003
Posts: 12
Thanks for all the replies.
JSP --> Action --> Service
I added a function to generate date_time in the Service class. This returns date_time to Action, which then sets the property.
Works like a champ.
Thanks again for all the useful pointers.
-- $uDhA
 
 
subject: Can javascript function set value of a form bean property?
 
Similar Threads
Problem in Client Side Validation using Struts
Problem in Client side validation using Struts
Re: using javascript in jsp's
Double Refresh of JSPs
Javascript and Struts Indexed Text Boxes