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 Workaround for dealing with String[] inputs 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 "Workaround for dealing with String[] inputs" Watch "Workaround for dealing with String[] inputs" New topic
Author

Workaround for dealing with String[] inputs

Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Hi,
as some of you may have noted, i've been trying to make a multiple input (String[] emails) work out. Finally i did! However i'm going through the following problem now.

I allow the user inputs how many emails for his/her company as he/she wants.
I receive this data in the ActionForm by a property


As said, it works fine, but there's a situation that is worrying me.

In this form (the one that the emails are in) i have some other fields like phone, companyName, etc...

If the user fill everything and leave, for example, the company name. My validate method in ActionForm will send the data back and won't allow the transaction to commit (naturally).

When this happens, he/she gets all the data he/she had already filled in all the inputs back (so he/she doesn't need to fill the in again).

BUT the email input value is not beeing returned. When the page comes back the user sees something like this in the email input field.



Instead of getting what he/she had filled first (that was probably something like this)


This probably happens because email is a String[] property and, consequently, i have the following getter for it.



So, is there any workaround for me to solve this situatio???


Leandro Melo
SCJP 1.4, SCWCD 1.4
Sree Jag
Ranch Hand

Joined: Oct 14, 2003
Posts: 77
Hi Melo,

I am confused here. You say that the emails(multiple) are stored in a String[] array.

Are there multiple text fields in the JSP?

If yes, then you can simply use logic:iterate on the String[] emails property.

If no, (i.e there is only one text field where the user can enter multiple emails seperated by a comma or some delimiter), then why do u need an String[] array to store them. It can simply be a string variable. Later on you can use StringTokenizer to identify individual mails.

If you can post your JSP code that wud make the problem more clear.

Thanks,
Seshu


Sree Jag<br />SCJP 1.4
Kinjal Sonpal
Ranch Hand

Joined: Jun 06, 2003
Posts: 96
Leandro, there are two possible solutions that strikes my mind straight away.

1) Scriptlet way. You should check for the length of the array in your jsp page and lay textfields accordingly. If the length is zero/ array is null (may be when the page is rendered first time), then put only one text field. Not so lucid and clean approach, though.

2) Indexed properties way. If you remember my very first post, earlier in the thread on similar topic, I had mentioned use of Indexed properties. Use indexed getter and setter for accessing individual elements and array accessors as you are having them right now. Then you can use Iterator tag for laying as many textfields as you require (refer to myformview.jsp in that thread). To make things easier, you can have your array pre-filled with atleast one element.

Try to work on these lines of thought. You should be able to strike a solid solution for this.

Btw, I remember having a conversation with one of my seniors (actually my elder brother!) for similar requirements. He was of the opinion that one could do away with dynamic table row generation using javascript with a server side row addition. He said, I'd not like to depend on client side javascript to add a new record. In such a situation the server is not really aware of the record addition at all. It's a little uncomfortable feeling. I fully agree with him on this note. Of course, here the context is not as critical as adding a new record (but I had that requirement), so you may not want to get into this, but you may want to keep this in mind.

HTH. If you need a code on indexed property, post back. Also see Indexed Properties How-to for indexed properties in struts. It's a good reference document.

Thanks and regards,
Kinjal Sonpal
[ June 19, 2004: Message edited by: Kinjal Sonpal ]
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Hi Kinjal,
i got two questions for you.

1 - I've searched the forum for using index properties (actually i even found a previous post of yours at http://www.coderanch.com/t/47929/Struts/Problems-Indexed-Properties-Repost), but i couldn't understand it very well yet. So, do u have some reference (or tutorial) or just some sample code on how i could acomplish that in situation such as the one i described?


2 - Yes, it's very nice to keep all control at sever side, but, as i said, i'll let the client (or user) choose how many emails he/she wants to register at one submit (so he/she doesn't get bored to have to register one email, then other email, then other email.... he/she registers all of them at once). So, i don't see how could i monitor that at server side, because i won't be able to know how many emails the user would like to register.


Well, i'd appreciate if you comment a little on the above said.

Thanks and regards,
ltcmelo
Kinjal Sonpal
Ranch Hand

Joined: Jun 06, 2003
Posts: 96
Leandro, here's answer to your first question.

I'll try to put things step by step. Note that the following code is not tested and executed, and is based on my understanding of the current context and iterator tag. However, I've extensively used a similar code in my project with great success and consistency.

MyForm.java
-----------
MyForm.jsp (The email form)
---------------------------
Here, we'd lay the controls using iterator tag. This'll allow proper re-rendering the controls if the validations fail. As explained in my earlier post, when the page is rendered for the first time, this may not render any control if the array is empty. So prepopulating the array with a dummy value will allow to render only one textfield.

If you require a readonly view of the form before final submission occurs, just replace <html: text> with <bean: write>, as explained in my post on the earlier thread.

One more thing. To the best of my knowledge, to make indexed properties go smoothly, the value of the id attribute in the iterate tag should match the indexed property in the form-bean. And the value of the property attribute should match the array accessor in form-bean. I've made the text bold for your easy understanding.

I hope I've not missed out any major detail. Please post back the results.

HTH.

Thanks and regards,
Kinjal Sonpal
[ June 21, 2004: Message edited by: Kinjal Sonpal ]
Kinjal Sonpal
Ranch Hand

Joined: Jun 06, 2003
Posts: 96
And here goes the second answer.
So, i don't see how could i monitor that at server side, because i won't be able to know how many emails the user would like to register.
That's true. You can't directly monitor the things at server-side, and that's what I was trying to suggest. And this where I get uncomfortable. May be I'm too conservative in my thinking about the security in the web world.

Anyway, in the current context, it seems to me that it's okay to have it done solely at client side, since it's really not so critical to have one or more emails added using javascript. But if you have some really critical work (like some line charges or new records), then I'd go for

1) Ask the user in the beginning, the number of lines to add, pass it on using a form submission, and then present the user with those many lines to edit.
OR
2) Provide the user with only one line at a time, giving add button to add one more line and submit the form. When the form is presented again, the previous lines are shown read-only and one more line is shown editable. This is similar to adding attachments to mail on most web based email services.
OR
3) Provide the user with some pre-determined(3-4 could be okay) number of editable lines. Then follow approach 2. This could allow you to save an additonal form submission on most occassion. However you should remember that even in this case you should not hard-code your jsp page for the number of lines. You should still run the iterator code. Let the fronting action decide how many lines it should have prefilled.

Hope this answers your question. There could also be some more approaches or there could be some flaws in the above as well. May be gurus would like to comment on this.

Thanks and regards,
Kinjal Sonpal

[ June 21, 2004: Message edited by: Kinjal Sonpal ]
[ June 21, 2004: Message edited by: Kinjal Sonpal ]
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Thanks for your time and attention Kinjal,
I'll try those strategies in 1 or 2 days (unfortuantelly, i won't be able to try them out rigth now) and let you know the results.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Workaround for dealing with String[] inputs
 
Similar Threads
best solution to questionnaire web app
Caching Problems with borwsers.
Request attributes missing
Cache control through response object.
Multilingual Email - help needed