struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject RE: collections in forms
Date Thu, 05 Dec 2002 14:58:46 GMT
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){
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) { = name;

	 * Sets the salary.
	 * @param salary The salary to set
	public void setSalary(String salary) {
		this.salary = salary;

<%@ page language="java"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
	<html:form action="/" >
		<logic:iterate id="bean" name="exampleListForm" property="beanList" 
			<html:text name="" property="<%=\"employee[\" + i \"].name\"%>">
			<html:text name="" property="<%=\"employee[\" + i 

-----Original Message-----
From: drew.zimber []
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?


To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message