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 :

If / Else dans mon Action


Sujet :

Struts 1 Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Étudiant
    Inscrit en
    Juin 2007
    Messages
    90
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2007
    Messages : 90
    Par défaut If / Else dans mon Action
    Bonjour,

    Aujourd'hui c'est le dernier problème que je pourrai vous poser donc je mise tous mes espoirs sur votre réponse

    Voila mon problème, j'ai une JSP avec une checkbox. Lorsque je la coche et que je soumet, je fait une requête à mes EJB. Lorsque je le décoche, j'ai un tableau qui apparaît où je peux faire une "recherche par critères".

    De ce fait, dans mon Action je test d'abord si ma checkbox est cochée, et ensuite si ce n'est pas le cas, quels champs sont remplis ou non.

    Le problème est que dans mon action j'utilise un if / else, et qu'une fois le if passé, j'ai le droit à une exception "null" (en faisant un getMessage()). Voyez donc mon code :

    JSP :

    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
    // Pour cacher / rendre visible le tableau de recherche par critères
    function ShowTab()
    {
      if(document.getElementById("chk1").checked ){
        document.getElementById("tab2").style.display = "none";
        document.getElementById("numero").value = "0";}
      else{document.getElementById("tab2").style.display = "block";
       document.getElementById("numero").value = "";}
    }
     
    <!-- Formulaire de recherches -->
              <html:form action="/Actions/doRechercherDemandesValidation.do">
     
    	<table cellspacing="6" >
              <tr>
                <td><html:checkbox property="allDemandes" onclick="ShowTab()" styleId="chk1" /></td>
                <td>Toutes les demandes</td>
              </tr>
           </table>
           <table cellspacing="6" >
              <tr>
               <td>Uniquement la(les) demande(s) ...</td>
              </tr>
               <tr>
     
    <!-- Calendrier -->
    <table class="ds_box" cellpadding="0" cellspacing="0" id="ds_conclass" style="display: none;">
    <tr><td id="ds_calclass">
    </td></tr>
    </table>
    <script language="javascript" src="/LivApp/Javascripts/calendrier.js"></script>
    <!-- Fin calendrier -->
     
                 <table class="tableau_recherche" align="center" id="tab2"  style="display: none;">
                  <tr>
                    <th>ayant le numéro (entrez le numéro de livraison) :</th>
                    <th><html:text styleId="numero" property="numero" value=""/></th>
                  </tr>
                  <tr>
                    <td>prévue(s) pour le (date de livraison) :</td>
                    <td><input onclick="ds_sh(this);" name="date" readonly="readonly" style="cursor:text"/></td>
                  </tr>
                  <tr>
                    <th>émise(s) par (entrez le nom du demandeur) :</th>
                    <th><input type"text"/></th>
                  </tr>
                   <tr>
                    <td>d'un type de composant spécifique : </td>
                    <td>
                      <bean:define id='listeDemandesValidation' type='java.util.Collection' name='tableauDemandesValidationActionForm' property='listeTypeDemandes'/>
                      <html:select name="tableauDemandesValidationActionForm" property="codeType" >
                      <html:option value="0">-- Tous les types --</html:option>
                      <html:options collection="listeDemandesValidation" property="codeType" labelProperty="libType" />
    		  </html:select>
                      </td>
                   </tr>
                  </table>
              </tr>
              </table>
              <br />
              <table align="center">
                <td><html:submit>Rechercher</html:submit></td>
                <td><html:button property="verMenu" onclick="javascript:annuler()" value="Annuler" /></td>
    	  </tr>
     
    	</table>
             </html:form>
    Mon Action :

    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
    package gest_livr_appli.Action;
     
    import org.apache.struts.action.*;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import gest_livr_appli.EJB.Delegate.LivraisonDelegate;
    import gest_livr_appli.DTO.LivraisonDto;
    import gest_livr_appli.ActionForm.LivraisonActionForm;
    import java.util.Arrays;
     
    public class doRechercherDemandesValidationAction extends Action{
     
      public ActionForward execute(ActionMapping actionMapping,
                                   ActionForm actionForm,
                                   HttpServletRequest httpServletRequest,
                                   HttpServletResponse httpServletResponse) {
     
        try {
              LivraisonActionForm resultatsDemandesValidationActionForm = (LivraisonActionForm) actionForm;
              LivraisonDelegate livraisonDelegate = new LivraisonDelegate();
     
              // Test de la recherche par Numéro
              String temp = httpServletRequest.getParameter("numero");
     
              // L'appli récupère le champ du numéro et le converti
              System.out.println(temp);
              int num = Integer.parseInt(temp);
              Integer numero = new Integer(num);
              LivraisonDto[] livraisonDto;
     
              // Test de la valeur de la checkbox
              String checkBox = httpServletRequest.getParameter("allDemandes");
              System.out.println(checkBox);
     
        // Si la checkbox est cochée, on sais quoi faire.
        if(checkBox.matches("on"))
                 {
                    livraisonDto = livraisonDelegate.livraisonFindAllWFV();
                    resultatsDemandesValidationActionForm.setlivListCollection(Arrays.asList(livraisonDto));
                  }
     
        // Sinon on fait autre chose mais le System.out ne passe même pas !!!
          else
              {  
              System.out.println("ça passe par le else");
             }
     }
              catch (Throwable ex) {
     
                // Le System.out m'affiche null (???)
               System.out.println(ex.getCause());
                return null;
             }
     
          return (actionMapping.findForward("afficherResultats"));
        }
     }

  2. #2
    Membre Expert Avatar de maxf1
    Profil pro
    Inscrit en
    Novembre 2006
    Messages
    1 229
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Moselle (Lorraine)

    Informations forums :
    Inscription : Novembre 2006
    Messages : 1 229
    Par défaut
    On met tous les espoirs de ton coté. Ta 10 minutes après je rentre chez moi, :-)

    Donne nous la stacktrace de ton erreur. Ca nous dira sur quoi il y a le nullPointer


    A vu de nez et avec un peu de chance, c'est livraisonDto qui est null.
    IL faudra le detail de la methode livraisonFindAllWFV

  3. #3
    Membre confirmé
    Profil pro
    Étudiant
    Inscrit en
    Juin 2007
    Messages
    90
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2007
    Messages : 90
    Par défaut
    Ok, ok , je me magne alors

    Voila la stacktrace :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    [Ljava.lang.StackTraceElement;@ef112]
    Edit : Je ne vois pas comment livraisonDto pourrait être null, puisque le if fonctionne très bien (la méthode livraisonFindAllWFV). C'est le else qui ne passe pas ...

    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
     public LivraisonDto livraisonFindByPrimaryKey(Integer numero) throws
          EJBException {
        try {
          return LivraisonDtoAssembler.createDto(livraisonHome.findByPrimaryKey(
              numero));
          //return assembleLivraisonDto(livraisonHome.findByPrimaryKey(numero));
        }
        catch (Exception e) {
          throw new EJBException(e.getMessage());
        }
      }
     
     public LivraisonDto[] livraisonFindAllWFV() {
        try {
          return LivraisonDtoAssembler.createDtos(livraisonHome.findAllWFV());
        }
        catch (Exception e) {
          throw new EJBException(e.getMessage());
        }
     
      }

  4. #4
    Membre Expert Avatar de maxf1
    Profil pro
    Inscrit en
    Novembre 2006
    Messages
    1 229
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Moselle (Lorraine)

    Informations forums :
    Inscription : Novembre 2006
    Messages : 1 229
    Par défaut
    Non je ne veux pas l'object stackTrace mais la trace de la stack.

    Tu n'as pas un gros paté d'explication ou tu ne comprend rien qui s'affiche dans ta console?

  5. #5
    Membre confirmé
    Profil pro
    Étudiant
    Inscrit en
    Juin 2007
    Messages
    90
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2007
    Messages : 90
    Par défaut
    Arf, oui désolé, où avais-je la tête

    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
    java.lang.NullPointerException
     
    	at gest_livr_appli.Action.doRechercherDemandesValidationAction.execute(doRechercherDemandesValidationAction.java:36)
     
    	at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
     
    	at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
     
    	at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
     
    	at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
     
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)1012
     
    null
     
     
     
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
     
    	at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1053)
     
    	at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:387)
     
    	at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
     
    	at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6291)
     
    	at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
     
    	at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:97)
     
    	at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3575)
     
    	at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2573)
     
    	at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:178)
     
    	at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:151)
    La ligne 36 de mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if(checkBox.matches("on"))

  6. #6
    Membre Expert Avatar de maxf1
    Profil pro
    Inscrit en
    Novembre 2006
    Messages
    1 229
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Moselle (Lorraine)

    Informations forums :
    Inscription : Novembre 2006
    Messages : 1 229
    Par défaut
    Voila ca veut dire que tu n'as pas le parametre "allDemandes" dans le request!

    Es-tu sur de ne pas avoir confondu parametre et attribut?

    Est-ce que dans la barre d'adresse tu as "allDemandes" qui est marqué?

  7. #7
    Membre confirmé
    Profil pro
    Étudiant
    Inscrit en
    Juin 2007
    Messages
    90
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2007
    Messages : 90
    Par défaut
    C'est bon ça fonctionne !!!

    Je ne sais pas pourquoi, mais en tout cas ... ça marche !
    J'ai juste repris exactement la fonction qui fonctionnait déjà, j'ai redeployé et maintenant ça tourne.

    Voici le code de la JSP :

    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
    <form action="/LivApp/Actions/doRechercherDemandesRecette.do">
              <div align="center">
     
                <select name="liste" onclick="javascript:dateChange(this)">
    			<option value="0">Toutes les demandes pour aujourd'hui</option>
    			<option value="1">Toutes les demandes pour demain</option>
                </select>
     
    <!-- Calendrier -->
    <table class="ds_box" cellpadding="0" cellspacing="0" id="ds_conclass" style="display: none;">
    <tr><td id="ds_calclass">
    </td></tr>
    </table>
    <script language="javascript" src="/LivApp/Javascripts/calendrier.js"></script>
    <!-- Fin calendrier -->
     
    <table>
      	<tr>
               <td>Date choisie : </td>
               <td><input type="text"  id="dateChoisie" name="dateChoisie" onclick="ds_sh(this);"/></td>
            </tr>
        </table>
         <br />
         <table>
                <tr>
                   <td><html:submit  value="Choisir" /></td>
                   <td><html:button property="" value="Annuler" onclick="javascript:annuler()"/></td>
                </tr>
              </table>
              </div>
    </form>
    Et le code de l'Action :

    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
    ublic ActionForward execute(ActionMapping actionMapping,
                                   ActionForm actionForm,
                                   HttpServletRequest httpServletRequest,
                                   HttpServletResponse httpServletResponse) {
     
        try {
              LivraisonActionForm resultatsDemandesValidationActionForm = (LivraisonActionForm) actionForm;
              LivraisonDelegate livraisonDelegate = new LivraisonDelegate();
              LivraisonDto[] livraisonDto;
     
              // Test de la valeur de la checkbox
              String checkBox = httpServletRequest.getParameter("allDemandes");
     
        if(checkBox != null && checkBox.matches("on"))
                 {
                    livraisonDto = livraisonDelegate.livraisonFindAllWFV();
                    resultatsDemandesValidationActionForm.setlivListCollection(Arrays.asList(livraisonDto));
                 }
     
          else  {
                   String temp = httpServletRequest.getParameter("numero");
                   if(temp != ""){
                     int num = Integer.parseInt(temp);
                     Integer numero = new Integer(num);
                   if(numero != null ){
                    livraisonDto = livraisonDelegate.livraisonFindByNumeroWFV(numero);
                    resultatsDemandesValidationActionForm.setlivListCollection(Arrays.asList(livraisonDto));
     
     
                   }
                   }
                  else{
     
           String sDate = httpServletRequest.getParameter("dateChoisie");
           SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
           Date date = formatter.parse(sDate);
           resultatsDemandesValidationActionForm.setlivListCollection
            (Arrays.asList(livraisonDelegate.livraisonFindByDateLivWFV(date)));
                   }
               }
         }
     
              catch (Throwable ex) {
     
                ex.printStackTrace();
                return null;
             }
     
          return (actionMapping.findForward("afficherResultats"));
        }
     }
    Encore merci pour tout !

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

Discussions similaires

  1. Récupérer le nom de l'action appelante dans mon ActionSupport
    Par Khaled.Noordin dans le forum Struts 2
    Réponses: 4
    Dernier message: 26/07/2011, 16h03
  2. Réponses: 1
    Dernier message: 10/10/2008, 22h50
  3. Réponses: 8
    Dernier message: 27/03/2008, 13h40
  4. [1.x] Création de plusieurs objets dans mon action.class.php
    Par lijko dans le forum Symfony
    Réponses: 2
    Dernier message: 30/12/2007, 13h37
  5. pourquoi je ne passe pas dans mon action
    Par fk04 dans le forum Struts 1
    Réponses: 5
    Dernier message: 03/10/2007, 21h20

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