File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes access form field element Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "access form field element" Watch "access form field element" New topic
Author

access form field element

Chris Pat
Ranch Hand

Joined: Aug 27, 2005
Posts: 156
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"/>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61084
    
  66

Please be sure to ask Struts questions in the Struts forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Chris Pat
Ranch Hand

Joined: Aug 27, 2005
Posts: 156
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?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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}" />


Merrill
Consultant, Sima Solutions
Chris Pat
Ranch Hand

Joined: Aug 27, 2005
Posts: 156
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

Joined: Feb 15, 2005
Posts: 4864
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

Joined: Aug 27, 2005
Posts: 156
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

Joined: Feb 15, 2005
Posts: 4864
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: access form field element