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 how to set value of text field 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 "how to set value of text field" Watch "how to set value of text field" New topic
Author

how to set value of text field

divya kundapur
Ranch Hand

Joined: Aug 21, 2007
Posts: 110

i have a hidden text field whose value is set inside a javascript. If i have to access this in struts action class how can i do it. and will i be able to access that fields value.


SCJP - 5.0
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Should be the same way as you do for the other fields. The action class should have a property for that field, as you do for the other fields.


Swastik
divya kundapur
Ranch Hand

Joined: Aug 21, 2007
Posts: 110

I included get and set methods for that hidden field, and when i access it in action class i get null. And the javascript is setting its value .
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Are you sure that java script event is getting fired properly? on submit click just for debugging purpose display the value of the hidden field using window.alert and make sure that its not blank or null.
divya kundapur
Ranch Hand

Joined: Aug 21, 2007
Posts: 110
yes i checked the hidden field value within javascript using alert and its getting set, but in the action class its giving null for string and 0 for int... since the value i m setting is like 2001 , 2002 etc. Do you think what i m doing is correct i.e setting its value in javascript and trying to access in action class and using getter and setter in actionform. Thanks for taking out time to reply to me
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Can you paste your html code here?
divya kundapur
Ranch Hand

Joined: Aug 21, 2007
Posts: 110


and hProdCat value is set in javascript and that part is working fine

i used getHProdCat and setHProdCat in actionform
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
You are doing it correctly Divya, except for a small thing..

You need to create the hidden field using struts html tag, so that it will get attached to the property defined in your form bean.

eg:

<html:hidden property="property_name_from_formbean" />

Once this tag is translated by the webserver/struts taglib combination, your javascript will be still bale to update this field without any problem. Please note that struts uses "property" attribute as name also : ie <html:hidden property="qwerty"> will get translated to <input type="hidden" name="qwerty">

So code your javascript accordingly.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6



Do you mean that the above line is invoking the action class and there you are not getting the value of variable val?
divya kundapur
Ranch Hand

Joined: Aug 21, 2007
Posts: 110
Hey Thanks Dawn Charangat , so when i access that field in javascript how should i refer it by its property right
divya kundapur
Ranch Hand

Joined: Aug 21, 2007
Posts: 110
Swastik Dey wrote:

Do you mean that the above line is invoking the action class and there you are not getting the value of variable val?


No not the above action class. That action is used to fetch values to the drop down menu
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

If that is the case , you are passing the value through query string the query string variable name should be same as the property name in you action class.

Lei Guoguo
Greenhorn

Joined: Mar 31, 2009
Posts: 26
in your code "window.frames['skills'].location.href" the hidden field's value is not used.

you can try
window.frames['skills'].location.href ="getSkillsListLabelBean.do?strProductCategory=" + val + "&hProdCat=" + document.getElementById("hProdCat").value;

do you sure the hidden field's value is not null?

i see your code "<input type="hidden" name="hProdCat" />" do not have value

I'm a chinese, my english is so bad, I need help. If you want to help me, you can send your question to my e-mail: lgg860911@yahoo.com.cn, I will help you, and improve my english. Thanks everyone.
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
Sure thing Divya.., let us know how you are faring.
divya kundapur
Ranch Hand

Joined: Aug 21, 2007
Posts: 110
after i used <html:hidden property="prodCat" />
other fields are is not displaying at all
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

I dont think its necessary to use the struts tag, but as you are passing the values through query string you have to include everything i.e. all the form element names and their values in the querystring.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Did you try the way I suggested

window.frames['skills'].location.href ="getSkillsListLabelBean.do?hProdCat=" + val

just use simple html tags. I dont think struts tag is necessary in this case.
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
Divya, I presume that you have done the below tasks:

1) Added the taglib to your webapp [ie, place the struts-html.tld file in your WEB-INF folder, this tld file comes with struts when you download]
2) Added this tld file as a reference in your JSP [ie, place this line at the top of your JSP <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>]

If you still are stuck, can you share the log
divya kundapur
Ranch Hand

Joined: Aug 21, 2007
Posts: 110

After i used this i.e value=""
<html:hidden property="hProdCat" value=""/>

the page is displaying properly with all fields but after the value is set in javascript i m still getting null..

or can i set the value of 'hProdCat in request or session object in jsp and then access it in action class
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

I dont think changing the tag from html to struts will help in resolving the issue, you will always get null. Your action class is getting through location.href , not through submit action. But your location.href is as follows
window.frames['skills'].location.href ="getSkillsListLabelBean.do?strProductCategory=" + val

so when the action class gets triggered there is no set/get methods as setProductCategory or getProductCategory, what is expected as per the querystring, but you have setHProdCat an getHProdCat
so your query string argument name should be same as property name used in the action class

means

window.frames['skills'].location.href ="getSkillsListLabelBean.do?strProductCategory=" + val

should be

window.frames['skills'].location.href ="getSkillsListLabelBean.do?hProdCat=" + val
divya kundapur
Ranch Hand

Joined: Aug 21, 2007
Posts: 110
I got the solution, the problem was i was not submitting it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to set value of text field
 
Similar Threads
Values in a text field
HTML Text Filed values
Drag text field on label and set label's text in textfield
Use of request dispatcher's include method
TextInputCallback..