• 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

access form field element

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
What is the syntax to access the form field valus within struts html tags? Is it recursive problem if I am using struts form tag also?
Something like this does not work:
<html:text property="someVar" value="this.form.field1.value"/>
 
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
Please be sure to ask Struts questions in the Struts forum.
 
Chris Pat
Ranch Hand
Posts: 156
  • 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:
Please be sure to ask Struts questions in the Struts forum.



Here now, thank you.
Is there an answer to the questions in this context?
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The expression "this.form.field1.value" is a JavaScript expression that will work only in a JavaScript function or event such as onclick. It will not work in a JSP value attribute. If your web application is a Servlet 2.4 application (you can tell by the DTD reference in the web.xml file) you can use an EL expression here. Otherwise, you can use a scriptlet. Example:

<html:text property="foo" value="${myFormBean.bar}" />
 
Chris Pat
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Merrill
Thank you. How/when is that myFormBean created? Is it automatic from the struts form tag. Can I refer to it by the form styleID?
<html:text property="foo" value="${myFormBean.bar}" />[/QB]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a form bean does not exist in its specified scope (request or session), Struts instantiates it prior to displaying the JSP. By "form bean", I mean the class specified in your struts-config.xml file.

Before we go any further, though, I need to understand more about what you're trying to accomplish. Are you trying to immediately change the value in one field when the user enters something in another? If so, you won't accomplish this at all by changing the value attribute of a Struts html:text tag. The only way to do this is with JavaScript, which means you would probably write some JavaScript code to be executed by the "onblur" event of the field being changed by the user that would then change the value of the field you want automatically changed. Example:

Note that the above should be "onblur" instead of "on_blur", but I had to change it to get around the JavaRanch filters. In this example, as soon as the user enters a value in field1 and moves the cursor away from it, field2 will be populated with the same value.

The reason the value attribute won't help you in this case is that it determines the value of a field before the HTML is sent back to the browser, so any changes made by the client will not have any effect.
[ December 06, 2007: Message edited by: Merrill Higginson ]
 
Chris Pat
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Merrill Higginson:
If a form bean does not exist in its specified scope (request or session), Struts instantiates it prior to displaying the JSP. By "form bean", I mean the class specified in your struts-config.xml file.

Before we go any further, though, I need to understand more about what you're trying to accomplish. Are you trying to immediately change the value in one field when the user enters something in another? If so, you won't accomplish this at all by changing the value attribute of a Struts html:text tag. The only way to do this is with JavaScript, which means you would probably write some JavaScript code to be executed by the "onblur" event of the field being changed by the user that would then change the value of the field you want automatically changed. Example:

Note that the above should be "onblur" instead of "on_blur", but I had to change it to get around the JavaRanch filters. In this example, as soon as the user enters a value in field1 and moves the cursor away from it, field2 will be populated with the same value.

The reason the value attribute won't help you in this case is that it determines the value of a field before the HTML is sent back to the browser, so any changes made by the client will not have any effect.

[ December 06, 2007: Message edited by: Merrill Higginson ]



Hello
I tried it with the formbean that is associated with the action of the form, and still the value is not adduced.

The background is that I have a js function triggered off the onSubmit that creates an array of the form element values and processes them. I would like to get that array, as it is processed not the form element values at the time of submit, as a single parameter to the Action. Because I want the values as processed in the js function sent to the Action, I am stuck in that I cant access that js array or iterate it as the value of the multiple <html:text property="sameVar" value="iteratedValueFromJSArray"/>

I may just have to rethink the approach, as I dont thinnk it can be done this way. Correct? tia.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still don't have a clear idea of what you're trying to accomplish, but since you're using an array of values, I suspect it would be helpful for you to read the question on indexed properties (question 6) of this forum's FAQ.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic