You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by totojack <to...@tin.it> on 2008/01/23 11:07:04 UTC

SOLVED - Re: [S2] Beans list in Dynamic form and ParametersInterceptor problem

Hi.

I've solved the problem. And the solution was... reading the documentation
with great attention!!!
In particular, from
http://struts.apache.org/2.0.11/docs/type-conversion.html, the paragraph
"Relationship to Parameter Names": 
1 - Use JavaBeans! The framework can only create objects if the objects obey
the JavaBean specification and provide no-arg constructions, as well as
getters and setters where appropriate
2 - Remember that person.name will call getPerson().setName(), but if in
order for the framework to create the Person object for you, a setPerson
must also exist

So: in the bean class, I've added a no-arg constructor; in the action class,
I've added a property Song and a setSong method. And now I'm able to fill
the list of beans modified from the form in the jsp, and use it in the
action.

I summerize all for future benefits.

-Problem: submit a dynamic form created with a list of beans and use these
beans in the action.

-BEAN
Provide a no-arg constructor and getters and setters for all properties.

public class Song implements Serializable {
    private String SongId;   
    private String StartDate;
    private String EndDate;
    private String StatusLoad;
    private String StatusSellable;
    
	public Song(){}
	
    public String getStartDate() {return StartDate;}
    public void setStartDate(String startDate) {StartDate = startDate;}
    public String getEndDate() {return EndDate;}
    public void setEndDate(String endDate) {EndDate = endDate;}
    public String getStatusLoad() {return StatusLoad;}
    public void setStatusLoad(String statusLoad) {StatusLoad = statusLoad;}
    public String getStatusSellable() {return StatusSellable;}
    public void setStatusSellable(String statusSellable) {StatusSellable =
statusSellable;}
    public String getSongId() {return SongId;}
    public void setSongId(String songId) {SongId = songId;}
} 

-ACTION
Provide the beans list property with get and set, and a bean property with
set.

public class ResubmitAction extends BaseAction {
    private List<Song> songs = new ArrayList();
	private Song song = null;
	public String resubmitCatalog() {
		...
		//use the beans list coming from the form
		if (this.songs != null) {
			log.debug("song list size "+songs.size());
			Iterator iter = this.songs.iterator();
			while (iter.hasNext()) {
				Song tmp = (Song) iter.next();
				if (tmp != null) {
					log.debug("------\n"+tmp.toString()+"\n------");
				}
			}
		}
	}
	public List<Song> getSongs() {return songs;}
    public void setSongs(List<Song> songs) {this.songs = songs;}
	public void setSong(Song song) {this.song = song;}
} 

-CONVERSION PROPERTIES FILE
Create a ActionName-conversion.properties file in the same path of the
action,
so in my case ResubmitAction-conversion.properties where
ResubmitAction.class is located.

Element_songs=console.beans.Song
CreateIfNull_songs=true 

In the first version of the file there was also the line
KeyProperty_songs=SongId
but this is useless because in the jsp I don't use the unique id
of the bean for iteration throw the list.

-JSP
Create the form using iterator tag and use stat.index to reference the
current iteration index.
Pay attention to the ognl expression.

<s:iterator id="Song" value="songs" status="stat">
<tr>
	<td>
		<s:hidden name="songs[%{#stat.index}].songId"/>
	</td>
</tr>
<tr>
	<td class="txtBold">START DATE</td>
	<td><jscalendar:jscalendar format="%d.%m.%Y"
name="songs[%{#stat.index}].startDate" /></td>
</tr>
<tr>
	<td class="txtBold">END DATE</td>      
	<td><jscalendar:jscalendar format="%d.%m.%Y"
name="songs[%{#stat.index}].endDate"/></td>
</tr>
<tr>
	<td class="txtBold">STATUS LOAD</td>
	<td><s:textfield theme="simple" name="songs[%{#stat.index}].statusLoad"
/></td>
</tr>
<tr>
	<td class="txtBold">STATUS SELLABLE</td>
	<td><s:textfield theme="simple" name="songs[%{#stat.index}].statusSellable"
/></td>
</tr>
</s:iterator> 

and the resulting html is (the beans list has 2 elements):

<input type="hidden" id="ResubmitCatalog_songs_0__songId" value="9121587"
name="songs[0].songId"/>
<input type="text" id="ResubmitCatalog_songs_0__startDate"
value="29.11.2007" name="songs[0].startDate"/>
<input type="text" id="ResubmitCatalog_songs_0__endDate" value="29.11.2007"
name="songs[0].endDate"/>
<input type="text" id="ResubmitCatalog_songs_0__statusLoad" value="Publish"
name="songs[0].statusLoad"/>
<input type="text" id="ResubmitCatalog_songs_0__statusSellable" value="1"
name="songs[0].statusSellable"/>
<input type="hidden" id="ResubmitCatalog_songs_1__songId" value="9121591"
name="songs[1].songId"/>
<input type="text" id="ResubmitCatalog_songs_1__startDate"
value="29.11.2007" name="songs[1].startDate"/>
<input type="text" id="ResubmitCatalog_songs_1__endDate" value="29.11.2007"
name="songs[1].endDate"/>
<input type="text" id="ResubmitCatalog_songs_1__statusLoad" value="Publish"
name="songs[1].statusLoad"/>
<input type="text" id="ResubmitCatalog_songs_1__statusSellable" value="1"
name="songs[1].statusSellable"/>


That's all. And I think it's enough...
It was hard, but with an happy end.
Thanks to all for replies to my first post.
-- 
View this message in context: http://www.nabble.com/-S2--Beans-list-in-Dynamic-form-and-ParametersInterceptor-problem-tp15016850p15038327.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org