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 :

Référence à une propriété d'un bean form


Sujet :

Struts 1 Java

  1. #1
    Membre extrêmement actif Avatar de Mister Nono
    Homme Profil pro
    Ingénieur Mathématiques et Informatique
    Inscrit en
    Septembre 2002
    Messages
    2 241
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur Mathématiques et Informatique
    Secteur : Santé

    Informations forums :
    Inscription : Septembre 2002
    Messages : 2 241
    Par défaut Référence à une propriété d'un bean form
    Avec le code suivant j'affiche la valeur de la propriété person du bean nommé ch03.hello et cela fonctionne :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    <logic:present name="ch03.hello" scope="request">
         <h2>
         Hello  <bean:write name="ch03.hello" property="person" />!<p>
         </h2>
    </logic:present>
    Mon soucis est que le bean du formulaire s'appelle :

    et qu'il se trouve dans le package :
    Comment cela se fait-il et comment faut-il comprendre cela ?

    Merci.

  2. #2
    Membre extrêmement actif Avatar de Mister Nono
    Homme Profil pro
    Ingénieur Mathématiques et Informatique
    Inscrit en
    Septembre 2002
    Messages
    2 241
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur Mathématiques et Informatique
    Secteur : Santé

    Informations forums :
    Inscription : Septembre 2002
    Messages : 2 241
    Par défaut
    Et bien dites donc : je pensais que ma question était extrèmenent simple.

    A+

  3. #3
    Expert confirmé

    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
    Par défaut
    Dans le struts-config.xml, le form-bean ne serait-il pas défini comme ceci ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
        <form-bean name="ch03.hello" type="ch03.hello.HelloForm"/>

  4. #4
    Membre Expert Avatar de willoi
    Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2006
    Messages
    1 355
    Détails du profil
    Informations personnelles :
    Âge : 52
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2006
    Messages : 1 355
    Par défaut
    Citation Envoyé par Mister Nono
    Avec le code suivant j'affiche la valeur de la propriété person du bean nommé ch03.hello et cela fonctionne :
    Quand tu dis cela fonctionne que veux-tu dire exactement ?
    On peut voir le code de ton ActionServlet, struts-config.xml, la jsp et ton bean ?

  5. #5
    Membre extrêmement actif Avatar de Mister Nono
    Homme Profil pro
    Ingénieur Mathématiques et Informatique
    Inscrit en
    Septembre 2002
    Messages
    2 241
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur Mathématiques et Informatique
    Secteur : Santé

    Informations forums :
    Inscription : Septembre 2002
    Messages : 2 241
    Par défaut
    Voilà les infos :

    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
      <?xml  version="1.0" encoding="ISO-8859-1" ?> 
       <!DOCTYPE  struts-config (View Source for full doctype...)> 
     - <!--      This is the Struts configuration file for the "Hello!" sample application
      --> f(clean);  
      - <struts-config>
      - <!--  ======== Form Bean Definitions ===================================   --> f(clean);  
      - <form-beans>
         <form-bean name="HelloForm"  type="ch03.hello.HelloForm" /> 
    
       </form-beans>
    
    
     - <!--  ========== Action Mapping Definitions ==============================   --> f(clean);  
      - <action-mappings>
      - <!--  Say Hello!   --> f(clean);  
      - <action path="/HelloWorld"  type="ch03.hello.HelloAction" name="HelloForm"  scope="request" validate="true" input="/hello.jsp">
         <forward name="SayHello"  path="/hello.jsp" /> 
    
       </action>
    
    
       </action-mappings>
    
    
     - <!--  ========== Message Resources Definitions ===========================   --> f(clean);  
        <message-resources parameter="ch03.hello.ApplicationResources" /> 
    
       </struts-config>

  6. #6
    Expert confirmé

    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
    Par défaut
    Peux-tu montrer le code de HelloAction stp ?

  7. #7
    Membre extrêmement actif Avatar de Mister Nono
    Homme Profil pro
    Ingénieur Mathématiques et Informatique
    Inscrit en
    Septembre 2002
    Messages
    2 241
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur Mathématiques et Informatique
    Secteur : Santé

    Informations forums :
    Inscription : Septembre 2002
    Messages : 2 241
    Par défaut
    Voilà tout cela :

    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
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
     
    package ch03.hello;
     
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    import javax.servlet.http.HttpServletResponse;
     
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionError;
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
     
    import org.apache.struts.util.MessageResources;
     
    import org.apache.commons.beanutils.PropertyUtils;
     
     
    /**
     * The <strong>Action</strong> class for our "Hello" application.<p>
     * This is the "Controller" class in the Struts MVC architecture.
     *
     * @author Kevin Bedell
     */
     
    public final class HelloAction extends Action {
     
        /**
         * Process the specified HTTP request, and create the corresponding HTTP
         * response (or forward to another web component that will create it).
         * Return an <code>ActionForward</code> instance describing where and how
         * control should be forwarded, or <code>null</code> if the response has
         * already been completed.
         *
         * @param mapping The ActionMapping used to select this instance
         * @param actionForm The optional ActionForm bean for this request (if any)
         * @param request The HTTP request we are processing
         * @param response The HTTP response we are creating
         *
         * @exception Exception if business logic throws an exception
         */
        public ActionForward execute(ActionMapping mapping,
                                             ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response)
        throws Exception {
     
            /*
             * This Action is executed either by calling
             *
             * /hello.do  - when loading the initial page
             * - or -
             * /hello.do?action=getName - whenever we post the form
             *
             */
     
            // If this is first time, go straight to page
            String action = request.getParameter("action");
              if (action == null) {
                  return (mapping.findForward("SayHello"));
            }
     
            // These "messages" come from the ApplicationResources.properties file
              MessageResources messages = getResources(request);
     
              /*
             * Validate the request parameters specified by the user
             * Note: Basic field validation done in HelloForm.java
             *       Business logic validation done in HelloAction.java
             */
              ActionErrors errors = new ActionErrors();
              String person = (String)
                PropertyUtils.getSimpleProperty(form, "person");
     
            String badPerson = "Atilla the Hun";
     
            if (person.equals(badPerson)) {
                errors.add("person",
                   new ActionError("ch03.hello.dont.talk.to.atilla", badPerson ));
                  saveErrors(request, errors);
                  return (new ActionForward(mapping.getInput()));
            }
     
            /*
             * Having received and validated the data submitted from the View,
             * we now update the model
             */
            HelloModel hm = new HelloModel();
            hm.setPerson(person);
            hm.saveToPersistentStore();
     
            /*
             * If there was a choice of View components that depended on the model
             * (oe some other) status, we'd make the decisoin here as to which
             * to display. In this case, there is only one View component.
             *
             * We pass data to the View components by setting them as attributes
             * in the page, request, session or servlet context. In this case, the
             * most appropriate scoping is the "request" context since the data
             * will not be nedded after the View is generated.
             *
             * Constants.HELLO_KEY provides a key accessible by both the
             * Controller component (i.e. this class) and the View component
             * (i.e. the jsp file we forward to).
             */
     
              request.setAttribute( Constants.HELLO_KEY, hm);
     
            // Remove the Form Bean - don't need to carry values forward 
            request.removeAttribute(mapping.getAttribute());
     
              // Forward control to the specified success URI
              return (mapping.findForward("SayHello"));
     
        }
    }
    et

    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
    83
     
    package ch03.hello;
     
    import javax.servlet.http.HttpServletRequest;
     
    import org.apache.struts.action.ActionError;
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionMapping;
     
    /**
     * Form bean for Chapter 03 sample application, "Hello World!"
     *
     * @author Kevin Bedell
     */
    public final class HelloForm extends ActionForm {
     
        // --------------------------------------------------- Instance Variables
     
        /**
         * The person we want to say "Hello!" to
         */
        private String person = null;
     
     
        // ----------------------------------------------------------- Properties
     
        /**
         * Return the person to say "Hello!" to
         *
         * @return String person the person to say "Hello!" to
         */
        public String getPerson() {
     
            return (this.person);
     
        }
     
        /**
         * Set the person.
         *
         * @param person The person to say "Hello!" to
         */
        public void setPerson(String person) {
     
            this.person = person;
     
        }
     
        // --------------------------------------------------------- Public Methods
     
        /**
         * Reset all properties to their default values.
         *
         * @param mapping The mapping used to select this instance
         * @param request The servlet request we are processing
         */
        public void reset(ActionMapping mapping, HttpServletRequest request) {
            this.person = null;
        }
     
        /**
         * Validate the properties posted in this request. If validation errors are
         * found, return an <code>ActionErrors</code> object containing the errors.
         * If no validation errors occur, return <code>null</code> or an empty
         * <code>ActionErrors</code> object.
         *
         * @param mapping The current mapping (from struts-config.xml)
         * @param request The servlet request object
         */
        public ActionErrors validate(ActionMapping mapping,
                                     HttpServletRequest request) {
     
            ActionErrors errors = new ActionErrors();
     
            if ((person == null) || (person.length() < 1))
                errors.add("person", new ActionError("ch03.hello.no.person.error"));
     
            return errors;
     
        }
     
    }
    et

    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
     
    package ch03.hello;
     
    /**
     * <p>This is a Model object which simply contains the name of the perons we
     * want to say "Hello!" to.<p>
     *
     * In a more advanced application, this Model component might update
     * a persistent store with the person name, use it in an argument in a web
     * service call, or send it to a remote system for processing.
     *
     * @author Kevin Bedell
     */
    public class HelloModel {
     
        // --------------------------------------------------- Instance Variables
     
        /**
         * The new person we want to say "Hello!" to
         */
        private String _person = null;
     
        // ----------------------------------------------------------- Properties
     
        /**
         * Return the new person we want to say "Hello!" to
         *
         * @return String person the person to say "Hello!" to
         */
        public String getPerson() {
            return this._person;
        }
     
        /**
         * Set the new person we want to say "Hello!" to
         *
         * @param person The new person we want to say "Hello!" to
         */
        public void setPerson(String person) {
     
            this._person = person;
     
        }
     
        // --------------------------------------------------------- Public Methods
     
        /**
         * This is a stub method that would be used for the Model to save
         * the information submitted to a persistent store. In this sample
         * application it is not used. 
         */
        public void saveToPersistentStore() {
     
            /*
             * This is a stub method that might be used to save the person's
             * name to a persistent store if this were a real application.
             *
             * The actual business operations that would exist within a Model
             * component would be dependedn upon the rquirements of the application.
             */
        }
     
     
     
    }
    et

    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
     
    package ch03.hello;
     
    /**
     * Constants to be used in the Hello World! Example
     * Chapter 03 of "Struts Kickstart"
     *
     * @author Kevin Bedell
     */
     
    public final class Constants {
     
       /**
         * The application scope attribute used for passing a HelloModel
         * bean from the Action class to the View component
         */
        public static final String HELLO_KEY = "ch03.hello";
     
    }
    et enfin

    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
     
    ; Application Resources for the "Hello" sample application
    ;
    ; Update History:
    ;
    ; Date      Whom   Action
    ; ========  ====   ======
    ; 09/15/02  kmb    Creation
    ;
     
    ; Validation and error messages for HelloForm and HelloAction
     
    ch03.hello.dont.talk.to.atilla=I told you not to talk to Atilla!!!
    ch03.hello.no.person.error=Please enter a <i>PERSON</i> to say hello to!
     
    ; Application Resources that are specific to the hello.jsp file
     
    hello.jsp.title=Hello - A first Struts program
    hello.jsp.page.heading=Hello World! A first Struts application
    hello.jsp.prompt.person=To whom would you like to say hello?
    A+

  8. #8
    Membre Expert Avatar de willoi
    Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2006
    Messages
    1 355
    Détails du profil
    Informations personnelles :
    Âge : 52
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2006
    Messages : 1 355
    Par défaut
    Je reitere ma question :

    Ton programme fonctionne en ecrivant "Hello quelquechose !" ?

    ou alors il affiche une page sans ce hello machintruc ?

  9. #9
    Inactif  
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    2 189
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2006
    Messages : 2 189
    Par défaut
    * Constants.HELLO_KEY provides a key accessible by both the
    * Controller component (i.e. this class) and the View component
    * (i.e. the jsp file we forward to).

    ta constante s'appelle ch3.hello donc ...

    dans ton action tu fais l'opération suivante :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    request.setAttribute( Constants.HELLO_KEY, hm);
    et dans ta jsp tu accèdes donc à ce qui à été mis dans ta request

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    name="ch03.hello" scope="request"

  10. #10
    Membre extrêmement actif Avatar de Mister Nono
    Homme Profil pro
    Ingénieur Mathématiques et Informatique
    Inscrit en
    Septembre 2002
    Messages
    2 241
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur Mathématiques et Informatique
    Secteur : Santé

    Informations forums :
    Inscription : Septembre 2002
    Messages : 2 241
    Par défaut
    Merci pour les explications, enfin j'ai compris.

    A+

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

Discussions similaires

  1. mettre à jour une propriété d'un bean
    Par dolfendo dans le forum Struts 1
    Réponses: 7
    Dernier message: 14/10/2008, 12h27
  2. Récuperer le nom d'une propriété d'un bean
    Par Cofondinde dans le forum Langage
    Réponses: 5
    Dernier message: 10/12/2007, 12h33
  3. Réponses: 2
    Dernier message: 02/07/2007, 22h55
  4. Réponses: 4
    Dernier message: 15/02/2007, 10h06
  5. Count sur une propriété d'un bean entité
    Par dmetzler dans le forum JSF
    Réponses: 11
    Dernier message: 14/02/2007, 21h35

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