wood burning stoves 2.0*
The moose likes Struts and the fly likes Submit form text fields (group) with same name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Submit form text fields (group) with same name" Watch "Submit form text fields (group) with same name" New topic
Author

Submit form text fields (group) with same name

Girish Varde
Greenhorn

Joined: Jul 09, 2004
Posts: 23
I want to display identical text fields in a form which should be as many as required, decided dynamically. Based on user's earlier entries or database, those many number of text fields to be displayed on the form. On submit, it should be available in action class as vector etc. Normal html:text property=" " cannot be used as multiple are needed with number of fields not known. I want to use struts' tags only not scriplets which i hate. I think I can use logic:iterate, but how to use it. Please help.
With checkbox, it's possible to read a group in String array.
With text fields, is it possible to read group of text fields with same name?
Thanks.
Regrds,
varde
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

With checkbox, it's possible to read a group in String array.
With text fields, is it possible to read group of text fields with same name?


You can do textfields the same way as checkboxes, but I believe you have to add the [#] portion to the property yourself. Further, you might have to use scriptlets to do this.

Here is my example. Please don't flame it with anti-scriptlet comments.

<% int count = 0; %>
<html:text property='<%= "customer[" + count++ + "]" %>'/>
<html:text property='<%= "customer[" + count++ + "]" %>'/>
<html:text property='<%= "customer[" + count++ + "]" %>'/>

in ActionForm
private String[] customer = new String[0];
public String[] getCustomer(){
return this.customer;
}
public void setCustomer(String[] c){
this.customer = c;
}


A good workman is known by his tools.
Girish Varde
Greenhorn

Joined: Jul 09, 2004
Posts: 23
Thanks Marc.

Thanks for your detailed expalination with example and that too instantly.
It worked.

By scriplet use I meant bulky code paragraphs difficult to read and maintain. What you have suggested is very moderate and just for indexing array. No much functionality is involved. Life is full of compromises and I think, this is a small compromises paying reach benefits.

I again realized the power of struts. Today you taught me addtional feature of action form. For checkbox, I was using <html:link> and request.getPararameterValues() to get String[]. Thanks to struts too.

God bless you.
Srikanth Shenoy
author
Ranch Hand

Joined: Jan 24, 2004
Posts: 184
Marc,

What you have done works. No problems at all.
And got nothing against scriptlets. Whatever works...

A slightly better solution however is to use indexed properties supported by Struts

Just do this:

<logic:iterate>
..
<html:text property="firstNames" indexed="true"/>
..
</logic:iterate>

It generates the texttag in html as you need:

<input type="text" property="firstNames[0]" />

<input type="text" property="firstNames[1]" />

and so on...

In the Form you can provide these methods:

public String getFirstNames(int i) {....}

public void setFirstNames(int i, String name) { ..... }

Not that you have done it wrong.
This is just a different way of achieving the same thing.


Srikanth Shenoy
Author of Struts Survival Guide : Basics to Best Practices
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Good suggestion.

I went to try and use it myself, to replace where I am using scriptlets, only to find that it requires being in a logic:iterate tag... which I modified... still no luck. Then I looked up the indexed attribute on the Struts website

indexed - Valid only inside of logic:iterate tag. If true then name of the html tag will be rendered as "id[34].propertyName". Number in brackets will be generated for every iteration and taken from ancestor logic:iterate tag. [RT Expr]

So in order for it to work, you need a collection of bean objects to iterate over. I think this thread's scenario does not use this type of data structure.

I could be very wrong here ( as I often am on this forum ), so any correction is much appreciated.
Srikanth Shenoy
author
Ranch Hand

Joined: Jan 24, 2004
Posts: 184
Hey Marc,

You have been of great help to folks in this forum
consistently and tireless helping out and contrary to what u said, you are always have the right solution.

Kudos to u
Girish Varde
Greenhorn

Joined: Jul 09, 2004
Posts: 23
Thanks Shrikant and Marc.

Actually, wanted to use form and associate form property with bean property only. So what Shrikant has suggested is exactly what I was looking for.

Thanks Shrikant for replying. In any project, we have entry and update form. For update form we have to preset field values of action form before display. In simple case, these values exactly relate to list obtained by firing query(Dao pattern). With this solution, it will automatically take care and reduce my code for setting property values.

I am again excited I can at least for now getting rid of scriplets.

Any body who is for Struts is automatically against scriplet. The reason being struts itself is evolved for implementing MVC and MVC means scripletless life. So it's not being a question of pro or against. Only thing is that if you are following development standards, your choice is influenced by these standards.

Coming back to our topic, I am now using two dimensional String arrary.
In the initial formdisplay, instead of blank text fields, it's displaying object values as [Ljava.lang.String;@489c42 for first row and [Ljava.lang.String;@10ac2a3 for second row. I will tell you guys what I did step by step.
-------------------------------------------------------------
1) Before jsp display, action class has following code.
String[] keywords = new String[noKeywords];
String[] searchEngines = new String[noSearchEngines];
String[][] keywordRankings = new String[noKeywords][noSearchEngines];

for(short i=0; i<noKeywords; i++) { // Keyword iteration

for(short j=0; j<noSearchEngines; j++) { // Se iteration

keywordRankings[i][j] = "" ; // nitially set to blank
}
}
keywordRankingForm.setKeywordRankings(keywordRankings);

request.getSession().setAttribute("KeywordRankingForm", keywordRankingForm);

2) In action form's property, i have private String[][] keywordRankings ;
keywordRankings = {//one dimensional String array with{} or {""} }

3)In action form's mehods I have,
public String[][] getKeywordRankings() {
return this.keywordRankings;
}
public void setKeywordRankings(String[][] keywordRankings) {
this.keywordRankings = keywordRankings;
}


4) In action form's constructor,
public KeywordRankingForm() {
super();
keywordRankings = new String[10][10];
}
--------------------------------------------------------------
I tried a lot, but fields are not showing me the values set by me and I don't have time to implement Shrikants technique as it's urgent. I being a new for struts, will take time to implement this new indexed feature.

Please help as asap.

Thanks in advance

Girish Varde
Girish Varde
Greenhorn

Joined: Jul 09, 2004
Posts: 23
This is just an extension of previous reply.
I forgot to give code in jsp.
5) In jsp form it is as

<% int keywordCount = 0; %>
<% int seCount = 0; %>
<div>
<center><h3>Keyword Ranking Form</h3></center>
</div>
<br>
<html:form action="/keyword/NewKeywordRankingForm" >
<table>

<table>

<logic:iterate name="KeywordList" id="keywordId" >
<tr>
<% seCount = 0; %>
<logic:iterate name="SearchEngineList" id="searchEngineId" >
<td>
<html:text property='<%= "keywordRankings[" + keywordCount + "][" + seCount++ + "]" %>' value=""/>
</td>
</logic:iterate>
<% keywordCount++ ; %>
</logic:iterate>

</table>

KeywordList and SearchEngineList are dummy lists whose iterations are used as for loop and values are not used currently.
Please note that if value="" attribute is used, it displays blank fields prperly. But this does not solve my problem as when I have to set them to desired values, I can not have value attribute or If you can suggest please help.

Thanks
Girish Varde
Girish Varde
Greenhorn

Joined: Jul 09, 2004
Posts: 23
This is just an extension of previous reply.
I forgot to give code in jsp.
5) In jsp form it is as

<% int keywordCount = 0; %>
<% int seCount = 0; %>
<div>
<center><h3>Keyword Ranking Form</h3></center>
</div>
<br>
<html:form action="/keyword/NewKeywordRankingForm" >
<table>

<table>

<logic:iterate name="KeywordList" id="keywordId" >
<tr>
<% seCount = 0; %>
<logic:iterate name="SearchEngineList" id="searchEngineId" >
<td>
<html:text property='<%= "keywordRankings[" + keywordCount + "][" + seCount++ + "]" %>' value=""/>
</td>
</logic:iterate>
<% keywordCount++ ; %>
</logic:iterate>

</table>

KeywordList and SearchEngineList are dummy lists whose iterations are used as for loop and values are not used currently.
Please note that if value="" attribute is used, it displays blank fields prperly. But this does not solve my problem as when I have to set them to desired values, I can not have value attribute or If you can suggest please help.

Thanks
Girish Varde
Girish Varde
Greenhorn

Joined: Jul 09, 2004
Posts: 23
With indexed attribute for <html:text> its allowing me to associate text fields with bean properties and get the property indexed in synchronization with bean collection. Good.
This is perfectly suited when you have to use one dimensional dynamic array for property.
But in my case, I want two dimensional array as I need to provide a table and not just a single column.
For this I tried to nest collection in collection and iterate. On it's own, it is not generating additional index but only one index and the field names are repeated for successive iteration. Bad luck.
Next, I tried to use scriplet (no other option ) and tried to generate new id="" by cancating outer iteration index with id name.
But I got error as expressions not allowed in id= . So sad.
So if you can take up this challenge of 2 dimensional text fields generation please go ahead and help me.

Thanks in advance.

Girish Varde.
Girish Varde
Greenhorn

Joined: Jul 09, 2004
Posts: 23
Hello Marc and Shrikanth,

I have realized that in some cases , there is no alternative to scriplet.

I colud set values in my my jsp form with 2-d array text fields using scriplets.

I am sorry for my anti-scriplet comments. I take my words back.

Anyway, my problem is sorted out. Thanks for your help.

varde
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Submit form text fields (group) with same name
 
Similar Threads
Struts2 Data Transfer
get parameter from another Form
Applying onChange and Short Cut key in a Submit button
Submit form text fields (group) with same name!!!
JTextField listener to enable button action