IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Struts 1 Java Discussion :

[Struts-Layout] Comment utiliser collectionInput


Sujet :

Struts 1 Java

  1. #1
    Membre à l'essai
    Inscrit en
    Avril 2007
    Messages
    25
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 25
    Points : 13
    Points
    13
    Par défaut [Struts-Layout] Comment utiliser collectionInput
    Bonjour tout le monde

    Je veux afficher une liste éditable avec la balise <layout:collectionInput> mais j'obtiens une erreur du type:
    Invalid use of collectionInput tag
    Est ce que quelqu'un peut m'aider à comprendre la source :
    "champs8" et "champ9" sont des champs de ma liste.
    J'utilise les même noms pour les attributs property et dans mon action form
    Voilà ci dessous
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    <%
        DaoNonconformite noncon=new DaoNonconformite();
        ArrayList ListeNonconAna = new ArrayList();
        ListeNonconAna=noncon.afNonconAnaTrait();
        session.setAttribute("ListeNonAnatrait",ListeNonconAna);   
    %>
    <layout:form action="nonconanatrait"> 
       <logic:present name="ListeNonAnatrait">
           <logic:empty name="ListeNonAnatrait">
                Aucune non conformité n est encore analysée;
            </logic:empty>
       </logic:present>
       <logic:present name="ListeNonAnatrait">
            <layout:collection name="ListeNonAnatrait" styleClass="FORM"   selectType="radio" selectName="selected2" selectProperty="champ2"  >
               <layout:collectionItem title="Coût en temps" property="champ8" sortable="true" />
               <layout:collectionInput title="Libellé de Traitement" formProperty="champ9" property="champ9"/>
               <layout:collectionInput title="Date du Traitement" formProperty="champ8"  property="champ8"/>
            </layout:collection>
       </logic:present>
       <layout:row>
           <layout:submit property="delete2" value="supprimer" />
       </layout:row>
    </layout:form>

  2. #2
    Expert éminent

    Femme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    5 793
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Juillet 2005
    Messages : 5 793
    Points : 7 778
    Points
    7 778
    Par défaut
    Dans l'ActionForm, il faut définir getter et setter indexés sur les propriétés champ8 et champ9.

    Voici un exemple pour une ArrayList userList de beans User ayant pour propriété nom, propriété que l'on veut pouvoir modifier via le tag collectionInput :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    	public String getNom(int index)
    	{
    		return ((User) this.userList.get(index)).getNom() ;
    	}
    	public void setNom(int index,String nom)
    	{
    		User user = (User) this.userList.get(index) ;
    		user.setNom(nom) ;
    		this.userList.set(index,user) ;
     	}
    Modératrice Java - Struts, Servlets/JSP, ...

  3. #3
    Membre à l'essai
    Inscrit en
    Avril 2007
    Messages
    25
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 25
    Points : 13
    Points
    13
    Par défaut
    Merci ,mais c'est pas tout à faire claire

    J'ai un peu de difficulté à comprendre l'exemple:
    Est-ce que UserListe est une propriété du bean User? Je demande cela parceque dans mon cas, la liste que j'ai mise en session dans la page Jsp n'a rien à voir avec l'action form dans laquelle je définis les propriétés champ8 et champ9.Ma liste contient les résultats d'une requête .
    J'ai l'intention d'afficher cette liste dans la collectionInput pour pouvoir modifier deux champs dans la liste et récupérer les valeurs modifiées dans champ8 et champ9 de l'action Form bean.
    Est ce je dois récupérer ma liste dans mon action form? si oui, comment?

    Merci encore pour tout.

  4. #4
    Expert éminent

    Femme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    5 793
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Juillet 2005
    Messages : 5 793
    Points : 7 778
    Points
    7 778
    Par défaut
    Effectivement, dans mon exemple, userList est une propriété de mon ActionForm.
    J'ai l'habitude de créer une Action qui initialise le formulaire par exemple comme ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    ArrayList userList = new ArrayList() ;
    userList.add(new User("Peter","Gabriel")) ;
    userList.add(new User("Dave","Gahan"));
    ...
    MonActionForm monActionForm = (MonActionForm) form ;
    monActionForm.setUserList(userList) ;
    Modératrice Java - Struts, Servlets/JSP, ...

  5. #5
    Membre à l'essai
    Inscrit en
    Avril 2007
    Messages
    25
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 25
    Points : 13
    Points
    13
    Par défaut
    Bonjour
    j'ai déclaré et initialisé ma liste comme tul'as indiqué;je crois que j'y suis presque
    mais j'obtient l'erreur suivante:
    Getter for property champ9[0] threw exception:java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

    Merci d'avance;

  6. #6
    Expert éminent

    Femme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    5 793
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Juillet 2005
    Messages : 5 793
    Points : 7 778
    Points
    7 778
    Par défaut
    As-tu modifié le code de la jsp ? si oui, peux-tu le montrer ?

    EDIT : et aussi celui de l'Action et de l'ActionForm ?
    Modératrice Java - Struts, Servlets/JSP, ...

  7. #7
    Membre à l'essai
    Inscrit en
    Avril 2007
    Messages
    25
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 25
    Points : 13
    Points
    13
    Par défaut
    Voici mon code Jsp textuellement ;j'y ai rien modifié
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
        <layout:collection name="ListeNonAnatraitement" styleClass="FORM"  indexId="noncon"  selectType="radio" selectName="selected2" selectProperty="champ2" sortAction="client"  >
                     <layout:collectionTitle title="Traitement">
                             <layout:collectionInput title="Libellé de Traitement" formProperty="champ9" property="champ9"/>
                             <layout:collectionInput title="Date du Traitement" formProperty="champ8"  property="champ8"/>
                         </layout:collectionTitle>
                         </layout:collection>

  8. #8
    Membre à l'essai
    Inscrit en
    Avril 2007
    Messages
    25
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 25
    Points : 13
    Points
    13
    Par défaut
    Voilà le code de mon action Form
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
     
    public class NonconanatraitForm extends org.apache.struts.action.ActionForm {
     
       private String selected2;
       private String delete2;
       private String champ8;//libelleé traitemement;
       private String champ9;//date traitement;
       private ArrayList traitList= new ArrayList();
        public NonconanatraitForm() {
            super();
            // TODO Auto-generated constructor stub
        }
     
     
     
        public String getSelected2() {
            return selected2;
        }
     
        public void setSelected2(String selected2) {
            this.selected2 = selected2;
        }
     
        public String getDelete2() {
            return delete2;
        }
     
        public void setDelete2(String delete2) {
            this.delete2 = delete2;
        }
     
        public String getChamp8() {
            return champ8;
        }
     
        public void setChamp8(String champ8) {
            this.champ8 = champ8;
        }
     
        public String getChamp9() {
            return champ9;
        }
     
        public void setChamp9(String champ9) {
            this.champ9 = champ9;
        }
     
     
    public String getChamp8(int index)
    {
    return ((NonconanatraitForm) this.getTraitList().get(index)).getChamp8() ;
    }
     
    public void setChamp8(int index,String champ8)
    {
    NonconanatraitForm form = (NonconanatraitForm) this.getTraitList().get(index) ;
    form.setChamp8(champ8) ;
    this.getTraitList().set(index,form) ;
     }
     
        public String getChamp9(int index)
    {
    return ((NonconanatraitForm) this.getTraitList().get(index)).getChamp9() ;
    }    
     
     
        public void setChamp9(int index,String champ9)
    {
    NonconanatraitForm form = (NonconanatraitForm) this.getTraitList().get(index) ;
    form.setChamp9(champ9) ;
    this.getTraitList().set(index,form) ;
     }
     
        public ArrayList getTraitList() {
            return traitList;
        }
     
        public void setTraitList(ArrayList traitList) {
            this.traitList = traitList;
        }

  9. #9
    Expert éminent

    Femme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    5 793
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Juillet 2005
    Messages : 5 793
    Points : 7 778
    Points
    7 778
    Par défaut
    Le code suivant est incorrect :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public String getChamp8(int index)
    {
    return ((NonconanatraitForm) this.getTraitList().get(index)).getChamp8() ;
    }
     
    public void setChamp8(int index,String champ8)
    {
    NonconanatraitForm form = (NonconanatraitForm) this.getTraitList().get(index) ;
    form.setChamp8(champ8) ;
    this.getTraitList().set(index,form) ;
     }
    traitList n'est pas une ArrayList d'objets NonconanatraitForm.
    Il faut remplacer NonconanatraitForm par le nom de la classe correspondant aux objets contenus dans ton ArrayList.
    Modératrice Java - Struts, Servlets/JSP, ...

  10. #10
    Membre à l'essai
    Inscrit en
    Avril 2007
    Messages
    25
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 25
    Points : 13
    Points
    13
    Par défaut
    j'ai toujours la même erreur.
    voilà mon action
    voilà mon action dans laquelle j'ai initialisé le ArrayListe de mon actionForm;
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
     
     public ActionForward execute(ActionMapping mapping, ActionForm  form,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
     
              HttpSession session = request.getSession();
     
            ArrayList traitListe=(ArrayList)session.getAttribute("ListeNonAnatraitement");
     
            NonconanatraitForm myform=(NonconanatraitForm)form;
     
            myform.setTraitList(traitListe);
     
            return mapping.findForward(SUCCESS);
     
        }
    }
    Voilà mon action que j'ai modifiée;
    La classe d'objets contenus dans ma liste s'appelle AfFichageUtil
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
     
    public void setChamp8(int index,String champ8)
    {
    AffichageUtil form = (AffichageUtil) this.getTraitList().get(index) ;
    form.setChamp8(champ8) ;
    this.getTraitList().set(index,form) ;
     }
     
        public String getChamp9(int index)
    {
    return ((AffichageUtil)this.getTraitList().get(index)).getChamp9() ;
    }    
     
     
        public void setChamp9(int index,String champ9)
    {
    AffichageUtil form = (AffichageUtil)this.getTraitList().get(index) ;
    form.setChamp9(champ9) ;
    this.getTraitList().set(index,form) ;
     }

  11. #11
    Expert éminent

    Femme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    5 793
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Juillet 2005
    Messages : 5 793
    Points : 7 778
    Points
    7 778
    Par défaut
    Comme je ne vois pas ce qui cloche, je poste un exemple complet dont tu pourras j'espère t'inspirer :

    extrait du struts-config.xml :
    Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <form-beans>
    	<form-bean name="userListForm" type="test.UserListForm" />
    </form-beans>
     
    <action-mappings>
       <action path="/initUserList" type="test.InitUserListAction"
    	 name="userListForm" validate="false" scope="session">
    	<forward name="success" path="/userList.jsp"/>
       </action>
       <action path="/processUserList" type="test.ProcessUserListAction"
    	 name="userListForm" validate="true" scope="session" input="/userList.jsp">
    	<forward name="success" path="/userList.jsp"/>
       </action>
    </action-mappings>
    classe User :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    public class User implements Serializable
    {
    	private String nom ;
    	private String prenom ;
     
    	User()
    	{
    		this.nom = null ;
    		this.prenom = null ;
    	}
    	User(String prenom,String nom)
    	{
    		this.nom = nom ;
    		this.prenom = prenom ;
    	}
    	public String getNom() {
    		return nom;
    	}
    	public void setNom(String nom) {
    		this.nom = nom;
    	}
    	public String getPrenom() {
    		return prenom;
    	}
    	public void setPrenom(String prenom) {
    		this.prenom = prenom;
    	}
    }
    ActionForm :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    public class UserListForm extends ActionForm
    {
        private ArrayList userList = new ArrayList();
        private String nom ;
     
        public ArrayList getUserList()
        {
    	return userList;
        }
        public void setUserList(ArrayList userList)
        {
       	this.userList = userList;
        }
        public String getNom(int index)
        {
    	return ((User) this.userList.get(index)).getNom() ;
        }
        public void setNom(int index,String nom)
        {
    	User user = (User) this.userList.get(index) ;
    	user.setNom(nom) ;
    	this.userList.set(index,user) ;
        }
    }
    classe Action initialisant le formulaire :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public class InitUserListAction extends Action
    {
        public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
       {
         	UserListForm userListForm = (UserListForm) form ;
     
        	ArrayList userList = new ArrayList() ;
        	userList.add(new User("Peter","Gabriel")) ;
        	userList.add(new User("Dave","Gahan")) ;
        	userList.add(new User("Martin","Gore")) ;
        	userList.add(new User("Roland","Orzabal")) ;
     
        	userListForm.setUserList(userList) ;
     
        	return(mapping.findForward("success"));
       }
    }
    extrait de la jsp :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    <layout:form action="processUserList">
    	<layout:collection property="userList" indexId="index" styleClass="FORM" align="center" width="100%">			
    	 	<layout:collectionInput title="Nom" property="nom" formProperty="nom" width="220px"/>
    		<layout:collectionItem title="Prenom" width="220px">
    			<layout:text property="userList[${index}].prenom" layout="false"/>
     		</layout:collectionItem>
    	</layout:collection>
    	<layout:submit>OK</layout:submit>
    </layout:form>
    classe s'exécutant au submit du formulaire :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public class ProcessUserListAction extends Action
    {
        public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
       {
        	UserListForm userListForm = (UserListForm) form ;
        	ArrayList userList = userListForm.getUserList() ;
        	for (int i=0; i<userList.size(); i++)
        	{
        		User user = (User) userList.get(i) ;
        		System.out.println(user.getNom()+"-"+user.getPrenom());
        	}
        	return(mapping.findForward("success"));
       }
    }
    Tu remarqueras que dans la jsp, pour pouvoir modifier le nom, j'ai utilisé le tag collectionInput et pour pouvoir modifier le prenom, j'ai utilisé le tag collectionItem combiné avec le tag text.
    L'avantage de la deuxième façon de coder, c'est que cela évite de coder les getters et setters sur les propriétés indexées.
    Peut-être que cette solution résoudra ton problème.
    En tout cas, dans mon projet de test, les deux solutions pour modifier les propriétés fonctionnent.

    En espérant n'avoir rien oublié et que ce code pourra t'aider à résoudre ton problème.
    Modératrice Java - Struts, Servlets/JSP, ...

  12. #12
    Membre à l'essai
    Inscrit en
    Avril 2007
    Messages
    25
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 25
    Points : 13
    Points
    13
    Par défaut
    Bonjour

    Je tenais à te remercier.Je dois reconnaître que , je ne serais pas aujourd'hui là où j'en suis sans ton aide .J'ai pu résoudre le problème.
    En fait j'ai eu l'idée d'initialiser ma liste dans le constructeur de ma classe action form.
    Voici la partie où j'ai fait l'initialisation
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
     
    public class NonconanatraitForm extends org.apache.struts.action.ActionForm {
     
       private String selected2;
       private String delete2;
       private String champ9;//libelétraitement
       private String champ8;//date
      private ArrayList ListeNonAnatraitement= new ArrayList();
        public NonconanatraitForm() {
            super();
     
            DaoNonconformite noncon=new DaoNonconformite();
          ListeNonAnatraitement=noncon.afNonconAnaTrait();
     
        }
     
     
     
        public String getSelected2() {
            return selected2;
        }
     
        public void setSelected2(String selected2) {
            this.selected2 = selected2;
        }
     
        public String getDelete2() {
            return delete2;
        }
     
        public void setDelete2(String delete2) {
            this.delete2 = delete2;
        }
     
     /*  public String getChamp8() {
            return champ8;
        }
     
       public void setChamp8(String champ8) {
            this.champ8 = champ8;
        }
     
        public String getChamp9() {
            return champ9;
        }*/
     
       public ArrayList getListeNonAnatraitement() {
            return this.ListeNonAnatraitement;
        }
     
        public void setListeNonAnatraitement(ArrayList ListeNonAnatraitement) {
            this.ListeNonAnatraitement = ListeNonAnatraitement;
        }
     
    public String getChamp8(int index)
    {
    return ((AffichageUtil)this.getListeNonAnatraitement().get(index)).getChamp8();
    }
     
    public void setChamp8(int index,String champ8)
    {
    AffichageUtil aff= (AffichageUtil)this.getListeNonAnatraitement().get(index) ;
    aff.setChamp8(champ8) ;
    this.getListeNonAnatraitement().set(index,aff) ;
     }
     
        public String getChamp9(int index)
    {
    return ((AffichageUtil)this.getListeNonAnatraitement().get(index)).getChamp9();
    }    
     
     
        public void setChamp9(int index,String champ9)
    {
    AffichageUtil aff = (AffichageUtil)this.getListeNonAnatraitement().get(index);
    aff.setChamp9(champ9) ;
    this.getListeNonAnatraitement().set(index,aff) ;
     }
     
     
     
    }
    Merci pour tout

  13. #13
    Expert éminent

    Femme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    5 793
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Juillet 2005
    Messages : 5 793
    Points : 7 778
    Points
    7 778
    Par défaut
    De rien.
    Modératrice Java - Struts, Servlets/JSP, ...

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Struts-Layout] Comment utiliser son propre SortUtil ?
    Par benamira dans le forum Struts 1
    Réponses: 4
    Dernier message: 09/08/2010, 17h54
  2. [ JSP ][ Struts-Layout ] Comment faire???
    Par Houbbba dans le forum Struts 1
    Réponses: 7
    Dernier message: 03/06/2010, 10h13
  3. Réponses: 1
    Dernier message: 29/09/2006, 12h00
  4. [Struts layout] comment faire un ascenseur dans un tableau
    Par chouchou93 dans le forum Struts 1
    Réponses: 4
    Dernier message: 21/02/2006, 09h08
  5. [Struts][Messages]Comment utiliser du code HTML
    Par Tueur_a_gage dans le forum Servlets/JSP
    Réponses: 4
    Dernier message: 12/12/2005, 10h35

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo