• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to set value of text field

 
divya kundapur
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
divya kundapur
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you paste your html code here?
 
divya kundapur
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

i used getHProdCat and setHProdCat in actionform
 
Dawn Charangat
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dawn Charangat
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure thing Divya.., let us know how you are faring.
 
divya kundapur
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after i used <html:hidden property="prodCat" />
other fields are is not displaying at all
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the solution, the problem was i was not submitting it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic