aspose file tools*
The moose likes Struts and the fly likes Struts iteration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts iteration" Watch "Struts iteration" New topic
Author

Struts iteration

Miguel Flecha
Ranch Hand

Joined: Jun 23, 2004
Posts: 85
Hi.
I'm doing an iteration over a collection of beans (IbtRegistros.java).
I use this code:

<bean efine id="registros" name="registros"/>
<logic:iterate name="registros">
<bean:write name="registros" property="email"/>
</logic:iterate>

It tells me that "there is not getter method for property email", but the getter method is defined in the bean.
I'm a bit confused with the differences of "id", "name", ...

Thanks a lot
Miguel
Pankaj Narang
Ranch Hand

Joined: Jun 07, 2004
Posts: 81
hi fleucha

I think you are not using the bean efine tag correctly, you have not given the formname to which registers belong to , i think it should be like this

<bean efine id="register" name= "Formname " property ="registers"/>

This will give you the value which i s returned by the getRegisters() method in ur formname if your method returns a collection of objects whose properties are defined tou can write them using <bean:write/>

Best Regards
Pankaj
Miguel Flecha
Ranch Hand

Joined: Jun 23, 2004
Posts: 85
the formname is the name of the bean class??
Pankaj Narang
Ranch Hand

Joined: Jun 07, 2004
Posts: 81
formane is the nem of your foem beans where your getter and setter of your registers is written
Miguel Flecha
Ranch Hand

Joined: Jun 23, 2004
Posts: 85
and what is "id"?
Pankaj Narang
Ranch Hand

Joined: Jun 07, 2004
Posts: 81
hi Flecha

i think you are very much confused with the attributes, id is the name given to the property of the form which you are difining in propety attribute.

Flech first let me know what is the scenario, what i understood from your code snippet is that you have a amethod in your bean class which returns the collection of objects which has the property called email and you want to print that whole collection using logic iterate.

If i am correct then i will tell you the easiest solution for this.

But confirm me i ma getting you correct or not
Best Regards
Pankaj
Miguel Flecha
Ranch Hand

Joined: Jun 23, 2004
Posts: 85
Exactly that.
I have a collection of "IbtRegistros", and one of the properties is "email"
Thanks a lot
Pankaj Narang
Ranch Hand

Joined: Jun 07, 2004
Posts: 81
hi flecha

So you want to know the way you can solve this problem.

See first make a geter and setter for the objects you getting in your collection .

Let us take you are getting a set of questions in which you have objects of question type which has different properties like text ,id etc.

In your question.jav write the getter and setter for you properties.

write the getter and setter like

private Question questions = new Question();
public Question getquestions()

public setquestions(Question question)

now while you iterate your collection for these objects you can write

<bean:write name ="formname" property="questions.questionid"/>

best Regards
Pankaj
Miguel Flecha
Ranch Hand

Joined: Jun 23, 2004
Posts: 85
That is defined, and called IbtRegistros.java
Pankaj Narang
Ranch Hand

Joined: Jun 07, 2004
Posts: 81
hi flecha

Now i think you are clear if you are not your just send me the code of your form bean and also the code of the class os the objects you are getting in the collection

or call me if you want more clarification

Best Regards
Pankaj

91-9818419298
Pankaj Narang
Ranch Hand

Joined: Jun 07, 2004
Posts: 81
Hi flecha

What is the doubt know,

be clear in you question

sorry
The no. is 91-9818418298
Miguel Flecha
Ranch Hand

Joined: Jun 23, 2004
Posts: 85
The bean:

package ibt.hibernate;

import ibt.hibernate.base.BaseIbtRegistros;

/**
* This is the object class that relates to the ibt_registros table.
* Any customizations belong here.
*/
public class IbtRegistros extends BaseIbtRegistros {

/*[CONSTRUCTOR MARKER BEGIN]*/
public IbtRegistros () {}

/**
* Constructor for primary key
*/
public IbtRegistros (java.lang.String _email) {
super(_email);
}

/**
* Constructor for required fields
*/
public IbtRegistros (
java.lang.String _email,
java.lang.String _funcionalidad,
java.lang.String _nombre,
java.lang.String _empresa,
java.lang.String _telefono,
java.lang.String _apellido1,
java.lang.String _password) {

super (
_email,
_funcionalidad,
_nombre,
_empresa,
_telefono,
_apellido1,
_password);
}
/*[CONSTRUCTOR MARKER END]*/
}



which extends from:



package ibt.hibernate.base;

import java.io.Serializable;


/**
* This class has been automatically generated by Hibernate Synchronizer.
* For more information or documentation, visit The Hibernate Synchronizer page
* at http://www.binamics.com/hibernatesync or contact Joe Hudson at joe@binamics.com.
*
* This is an object that contains data related to the ibt_registros table.
* Do not modify this class because it will be overwritten if the configuration file
* related to this class is modified.
*
* @hibernate.class
* table="ibt_registros"
*/
public abstract class BaseIbtRegistros implements Serializable {

public static String PROP_PASSWORD = "Password";
public static String PROP_EMAIL = "Email";
public static String PROP_APELLIDO1 = "Apellido1";
public static String PROP_NOMBRE = "Nombre";
public static String PROP_EMPRESA = "Empresa";
public static String PROP_APELLIDO2 = "Apellido2";
public static String PROP_ESTADO = "Estado";
public static String PROP_TELEFONO = "Telefono";
public static String PROP_FUNCIONALIDAD = "Funcionalidad";


private int hashCode = Integer.MIN_VALUE;

// primary key
private java.lang.String _email;

// fields
private java.lang.Integer _estado;
private java.lang.String _funcionalidad;
private java.lang.String _apellido2;
private java.lang.String _nombre;
private java.lang.String _empresa;
private java.lang.String _telefono;
private java.lang.String _apellido1;
private java.lang.String _password;


// constructors
public BaseIbtRegistros () {}

/**
* Constructor for primary key
*/
public BaseIbtRegistros (java.lang.String _email) {
this.setEmail(_email);
}

/**
* Constructor for required fields
*/
public BaseIbtRegistros (
java.lang.String _email,
java.lang.String _funcionalidad,
java.lang.String _nombre,
java.lang.String _empresa,
java.lang.String _telefono,
java.lang.String _apellido1,
java.lang.String _password) {

this.setEmail(_email);
this.setFuncionalidad(_funcionalidad);
this.setNombre(_nombre);
this.setEmpresa(_empresa);
this.setTelefono(_telefono);
this.setApellido1(_apellido1);
this.setPassword(_password);
}



/**
* Return the unique identifier of this class
* @hibernate.id
* generator-class="vm"
* column="EMAIL"
*/
public java.lang.String getEmail () {
return _email;
}

/**
* Set the unique identifier of this class
* @param _email the new ID
*/
public void setEmail (java.lang.String _email) {
this._email = _email;
this.hashCode = Integer.MIN_VALUE;
}


/**
* Return the value associated with the column: ESTADO
*/
public java.lang.Integer getEstado () {
return _estado;
}

/**
* Set the value related to the column: ESTADO
* @param _estado the ESTADO value
*/
public void setEstado (java.lang.Integer _estado) {
this._estado = _estado;
}


/**
* Return the value associated with the column: FUNCIONALIDAD
*/
public java.lang.String getFuncionalidad () {
return _funcionalidad;
}

/**
* Set the value related to the column: FUNCIONALIDAD
* @param _funcionalidad the FUNCIONALIDAD value
*/
public void setFuncionalidad (java.lang.String _funcionalidad) {
this._funcionalidad = _funcionalidad;
}


/**
* Return the value associated with the column: APELLIDO2
*/
public java.lang.String getApellido2 () {
return _apellido2;
}

/**
* Set the value related to the column: APELLIDO2
* @param _apellido2 the APELLIDO2 value
*/
public void setApellido2 (java.lang.String _apellido2) {
this._apellido2 = _apellido2;
}


/**
* Return the value associated with the column: NOMBRE
*/
public java.lang.String getNombre () {
return _nombre;
}

/**
* Set the value related to the column: NOMBRE
* @param _nombre the NOMBRE value
*/
public void setNombre (java.lang.String _nombre) {
this._nombre = _nombre;
}


/**
* Return the value associated with the column: EMPRESA
*/
public java.lang.String getEmpresa () {
return _empresa;
}

/**
* Set the value related to the column: EMPRESA
* @param _empresa the EMPRESA value
*/
public void setEmpresa (java.lang.String _empresa) {
this._empresa = _empresa;
}


/**
* Return the value associated with the column: TELEFONO
*/
public java.lang.String getTelefono () {
return _telefono;
}

/**
* Set the value related to the column: TELEFONO
* @param _telefono the TELEFONO value
*/
public void setTelefono (java.lang.String _telefono) {
this._telefono = _telefono;
}


/**
* Return the value associated with the column: APELLIDO1
*/
public java.lang.String getApellido1 () {
return _apellido1;
}

/**
* Set the value related to the column: APELLIDO1
* @param _apellido1 the APELLIDO1 value
*/
public void setApellido1 (java.lang.String _apellido1) {
this._apellido1 = _apellido1;
}

/**
* Return the value associated with the column: PASSWORD
*/
public java.lang.String getPassword () {
return _password;
}

/**
* Set the value related to the column: PASSWORD
* @param _password the PASSWORD value
*/
public void setPassword (java.lang.String _password) {
this._password = _password;
}


public boolean equals (Object obj) {
if (null == obj) return false;
if (!(obj instanceof ibt.hibernate.base.BaseIbtRegistros)) return false;
else {
ibt.hibernate.base.BaseIbtRegistros mObj = (ibt.hibernate.base.BaseIbtRegistros) obj;
if (null == this.getEmail() || null == mObj.getEmail()) return false;
else return (this.getEmail().equals(mObj.getEmail()));
}
}


public int hashCode () {
if (Integer.MIN_VALUE == this.hashCode) {
if (null == this.getEmail()) return super.hashCode();
else {
String hashStr = this.getClass().getName() + ":" + this.getEmail().hashCode();
this.hashCode = hashStr.hashCode();
}
}
return this.hashCode;
}


public String toString () {
return super.toString();
}

}



In the Struts Action:

List registros=session.createQuery("from IbtRegistros as a where a.estado='0'").list();

Iterator registros_i = registros.iterator();
Iterator cambios_i=cambios.iterator();

request.setAttribute("registros",registros_i);


And in the JSP:

...

<bean efine id="registros" name="registros"/>

<logic:iterate id="registro" formname="ibt.hibernate.IbtRegistros" name="registros">

<tr>

<td><center><bean:write name="registro" property="email"/></center></td>
<td><center><bean:write name="registro" property="telefono"/></center></td>
<td><center><html:link href="">Validar registro</html:link></center></td>
</tr>

...
Pankaj Narang
Ranch Hand

Joined: Jun 07, 2004
Posts: 81
you wont have a getter setter fo registros in your foembean whcih you using for this request then how you are using bean efine, you ahould have the getter setter in you form bean for registros .

Best regards
Pankaj
Miguel Flecha
Ranch Hand

Joined: Jun 23, 2004
Posts: 85
But I have used this bean with <bean:write name="registro" property="email"/> and it works...
Pankaj Narang
Ranch Hand

Joined: Jun 07, 2004
Posts: 81
is it working now .
If working fine than its ok other wise let me give one more simple way to do this a very simple way.

Have you heard about diaplay taglib , you can use this tag library in this case very efectively.

If your problemis solved it is wel and good otherwise let me tel you the display taglib way of doing this it is just two line of code to do this iteration.

Best regards
Pankaj
Miguel Flecha
Ranch Hand

Joined: Jun 23, 2004
Posts: 85
the problem of the iteration is not solved, but I think is a problem with the "id". I don't know what to put there...
Miguel Flecha
Ranch Hand

Joined: Jun 23, 2004
Posts: 85
I will explain my problem again. This is my code

<logic:iterate id="???" name="registros">
<bean:write name="???" property="email"/>
</logic:iterate>

where "registros" is an Iterator of "IbtRegistros".

I tried putting "IbtRegistros" on the id and name fields, but got the following error: "Cannot find bean IbtRegistros in any scope"

I also put the fully qualified name of IbtRegistros in the "type" attribute

I thought that maybe the bean was not working correctly, but I passed a bean instead of an iterator of beans, used bean:write, and it worked perfectly.
Could you please tell me what to put in those fields?
Thanks a lot
Regards from Spain
Miguel
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Correct me if I'm wrong, but if I'm not mistaken, you're suppose to pass in a collection of beans, not an Iterator of beans.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Miguel Flecha
Ranch Hand

Joined: Jun 23, 2004
Posts: 85
You can pass a Collection, an array, an Iterator,... you have many posibilities...
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

The id and name attributes are indeed very confusing at first. You should not put the same value for both. It helps to understand the type of code the iterate tag generates.



If you don't specify the scope, Struts will search all context collections for the bean specified by the name attribute.

I'll leave it to you to connect the dots...


Junilu - [How to Ask Questions] [How to Answer Questions]
Miguel Flecha
Ranch Hand

Joined: Jun 23, 2004
Posts: 85
I did something similar and it tells me that the bean is not found in any scope :-(
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts iteration