You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by sh...@ubs.com on 2002/12/10 09:32:59 UTC
RE: collections in forms((correction in Form code to make it work))
Hi,
There was a problem in the sample FOrm Bean code I have given.
The getEmployee (int Index){
}method should be modified as follows
/*****************************
instead of if(index >= beanList.size()){
beanList.add(new Employee());
}
now I have made it
while(index >= beanList.size()){
beanList.add(new Employee());
}
/***************************
public class EmployeeListForm extends ActionForm {
public Employee getEmployee(int index){
while(index >= beanList.size()){
beanList.add(new Employee());
}
return (Employee)beanList.get(index);
}
}
-----Original Message-----
From: drew.zimber [mailto:drew.zimber@shaws.com]
Sent: Friday, December 06, 2002 5:36 PM
To: drew.zimber; Sakhare, Shirishchandra; drew.zimber
Subject: RE: collections in forms
hi,
Thank you for the explanation and sample code. Everything seems to make
sense but i have implmented your example and cannot get it working. The
biggest reason is because the jsp does not like your custom tag to display
the values. could you take a look at it again and see if it is correct:
JSP
<%@ page language="java"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:html>
<html:form action="/" >
<logic:iterate id="bean" name="exampleListForm" property="beanList"
indexId="i"/>
<html:text name="" property="<%=\"employee[\" + i \"].name\"%>">
<html:text name="" property="<%=\"employee[\" + i
\"].salary\"%>">
</logic:iterate>
</html:form>
</html:html>
thank you!
dz
-----Original Message-----
From: Drew Zimber [mailto:drew.zimber@shaws.com]
Sent: Thursday, December 05, 2002 4:55 PM
To: 'Struts Users Mailing List'
Subject: RE: collections in forms
thanks for the reply...i was busy doing something else but i will get back
to you tomorrow after some testing
dz
-----Original Message-----
From: shirishchandra.sakhare@ubs.com
[mailto:shirishchandra.sakhare@ubs.com]
Sent: Thursday, December 05, 2002 9:59 AM
To: drew.zimber@shaws.com; struts-user@jakarta.apache.org
Cc: nitin.karmuse@hays.com
Subject: RE: collections in forms
Hi,
If u are displaying the screen as text fields(using html:text tag),what ever
u
show on screen will be retained as all the data gets resubmitted back and
struts repopulates teh form again with the data.
But if u are showing a read only data on screen using bean:write tag,then
the
data will nto be retained as bean write tag does not include parameters in
request.So in this case if u want to retain the data, u should also use
html:hidden for each bean write u want to retain so that when the form is
submitted, the data is resent back.
I am giving some sample code...GO through it.Hope it helps.
********************************************************
//Form Class
import java.util.ArrayList;
import java.util.List;
import org.apache.struts.action.ActionForm;
public class ExampleListForm extends ActionForm {
//A list of Emp beans
private List beanList = new ArrayList();
public List getBeanList(){
return beanList;
}
public void setBeanList(List list){
beanList = list;
}
//very imp.
//give indexed access to the beans
public Employee getEmployee(int index){
//very imp
//when a jsp is submited , then while auto populating the form,this
will ensure that
// teh fporm is populated properly.
if(index >= beanList.size()){
beanList.add(new Employee());
}
return (Employee)beanList.get(index);
}
public void setEmployee(int index,Employee emp){
beanList.set(index,emp);
}
}
***********************************************
Bean class
public class Employee {
private String name;
private String salary;
/**
* Returns the name.
* @return String
*/
public String getName() {
return name;
}
/**
* Returns the salary.
* @return String
*/
public String getSalary() {
return salary;
}
/**
* Sets the name.
* @param name The name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* Sets the salary.
* @param salary The salary to set
*/
public void setSalary(String salary) {
this.salary = salary;
}
}
****************************************
JSP
<%@ page language="java"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:html>
<html:form action="/" >
<logic:iterate id="bean" name="exampleListForm" property="beanList"
indexId="i"/>
<html:text name="" property="<%=\"employee[\" + i \"].name\"%>">
<html:text name="" property="<%=\"employee[\" + i
\"].salary\"%>">
</logic:iterate>
</html:form>
</html:html>
***************************************************************************
-----Original Message-----
From: drew.zimber [mailto:drew.zimber@shaws.com]
Sent: Thursday, December 05, 2002 3:07 PM
To: struts-user
Subject: collections in forms
hey all...
i've used struts quite a bit now, but im not too well versed with
manipulating collections in form objects. I had thought there was a way to
set up your forms/pages so that if you display a collection on screen and
submit back, the form in the least retains the collection in the action
class (an update would be nice too, but step by step i guess).
everytime i submit back to a form, the Collection comes back null. Can
anyone give me some info/links/ideas on this so i dont have to rely on the
session so much?
thx!
dz
--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>
--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>
--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>