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

Servlets/JSP Java Discussion :

JSP, Taglib, comment faire quelque chose de propre


Sujet :

Servlets/JSP Java

  1. #1
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2008
    Messages
    108
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2008
    Messages : 108
    Points : 56
    Points
    56
    Par défaut JSP, Taglib, comment faire quelque chose de propre
    Bonjour à tous .

    Je ne suis encore pas vraiment bon en Java, et souhaiterais faire quelque chose de propre.

    En gros, je vous explique mon soucis:
    Dans une page JSP, j'ai ceci:
    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
    <c:forEach items="${listInter}" var="inter">
                            
                            <tr class='uneLigne'>
                                
                                    <td class='id'>${inter.id}</td>
                                    <td class='campus'>
                                        <c:if test="${not empty inter.campus}">
                                            <c:out value="${inter.campus.ville}"/> 
                                        </c:if>
                                        ${inter.promotion}
                                    </td>
                                    <td class='sujet'>${inter.sujet}</td>
                                    <td class='datedebut'>${inter.dateDebut}</td>
                                    <td class='datefin'>${inter.dateFin}</td>
                                    <td class='statut'>
                                        <c:if test="${dateActuelle<=inter.dateFin&&dateActuelle>=inter.dateDebut}">En cours</c:if>
                                        <c:if test="${dateActuelle<inter.dateDebut}">A venir</c:if>
                                        <c:if test="${dateActuelle>inter.dateFin}">Terminé</c:if>
                                    </td>
                                    <td class='note'>note</td>
                                    <td class="editdelete">
                                        <a href="editerIntervention?id=${inter.id}" title="Editer une intervention"><img src="../img/edit.png" alt="Editer" width="20" /></a>
                                        <a href="supprimerIntervention?id=${inter.id}" title="Supprimer une intervention" onclick="if ( confirm( 'Vous etes sur le point de supprimer cet &eacute;l&eacute;ment.\n\nAnnuler pour abandonner, \nOK pour le supprimer.' ) ) { return true;}return false;"><img src="../img/close.png" alt="Supprimer" width="20" /></a>
                                    </td>
                            </tr>
                        </c:forEach>
    Tout cela marche parfaitement, je récupère toutes ces informations dans mon Servlet via ceci:

    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
    @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
     
            Sta sta = staDao.getStaByIdBooster((Long)request.getSession().getAttribute("idbooster"));
     
            List<Intervention> list = interventionDao.rechercheInterventionsParSta(sta);
            request.setAttribute("listInter", list);
     
            Calendar date = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            request.setAttribute("dateActuelle", sdf.format(date.getTime()));
     
            request.getRequestDispatcher("intervention.jsp").forward(request, response);
     
        }
    Mais voilà mon problème:
    Dans le fichier jsp se trouve le mot "note" que j'ai souligné et mis en gras. Mon but, serait, en fonction de l'intervention qui est affichée, faire une suite d'opérations qui ressembleraient au code ci-dessous ... Mais tout ceci, proprement sous forme de balises html et tout ...

    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
    Long idInter = Long.decode(request.getParameter("id"));
            Intervention inter = interventionDao.getInterventionById(idInter);
            request.setAttribute("interDetails", inter);
     
            // On va lister les notes par interventions
            List<Note> notes = noteDao.rechercheNotesParIntervention(inter);
            int nbrNotes = notes.size();
            float noteSta = 0;
            float noteSlide = 0;
            float noteEvenement = 0;
     
            if (nbrNotes > 0) {
                for (Note noteDetail : notes) {
     
                    float noteStaProvisoire = 0;
                    float noteSlideProvisoire = 0;
                    noteStaProvisoire = noteDetail.getStaConnaissances() + noteDetail.getStaPedagogie() + noteDetail.getStaReponses();
                    noteStaProvisoire = noteStaProvisoire / 3;
                    noteSlideProvisoire = noteDetail.getSlideContenu() + noteDetail.getSlideExemples() + noteDetail.getSlidePresentation();
                    noteSlideProvisoire = noteSlideProvisoire / 3;
     
                    noteSta = noteSta + noteStaProvisoire;
                    noteSlide = noteSlide + noteSlideProvisoire;
     
                }
                noteSta = noteSta / nbrNotes;
                noteSlide = noteSlide / nbrNotes;
                noteEvenement = noteSlide + noteSta;
                noteEvenement = noteEvenement / 2;
     
     
                request.setAttribute("noteSta", df.format(noteSta)+"/5");
                request.setAttribute("noteSlide", df.format(noteSlide)+"/5");
                request.setAttribute("noteEvenement", df.format(noteEvenement)+"/5");
     
            } else {
     
                request.setAttribute("noteSta", "<span style='font-size: 13px; margin-left: 10px;'><i>Indisponible</i></span>");
                request.setAttribute("noteSlide", "<span style='font-size: 13px; margin-left: 10px;'><i>Indisponible</i></span>");
                request.setAttribute("noteEvenement", "<span style='font-size: 13px; margin-left: 10px;'><i>Indisponible</i></span>");
     
            }
     
            request.setAttribute("nbrNotes", nbrNotes);
    Le but serait donc, en fonction de "l'intervention" (table intervention), afficher sa note à l'emplacement où se trouve le mot "note" dans le JSP ci-dessus. Cette note sera donc calculée en faisant une suite de petits calculs bateaux à partir de la liste de "notes" (table note) correspondantes à l'intervention.

    Est-ce possible ? Et si oui, comment ?

    Je vous remercie d'avance pour votre aide

  2. #2
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 313
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 313
    Points : 9 529
    Points
    9 529
    Billets dans le blog
    1
    Par défaut
    C'est possible et un Tag me paraît bien adapté.
    Il faudra créer ta classe étendant TagSupport et définir la méthode doEndTag.
    Pour les arguments, il faut les définir en fonction de tes besoins mais à priori, l'id concerné suffit.
    Ensuite, il faut créer un fichier tld pour référencer ton tag et utiliser le tag dans ta page au niveau de la colonne... rien que du classique.

  3. #3
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2008
    Messages
    108
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2008
    Messages : 108
    Points : 56
    Points
    56
    Par défaut
    Je jette un œil dès ce soir là dessus .

  4. #4
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2008
    Messages
    108
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2008
    Messages : 108
    Points : 56
    Points
    56
    Par défaut
    Et voilà, cela marche parfaitement .

    Je montre quand même mes fichiers, on sait jamais, ça peut toujours servir:

    Alors, dans mon JSP, j'inclus mon fichier tld:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <%@taglib uri="/tlds/taglib.tld" prefix="tag21" %>
    Il aura donc pour préfixe tag21...
    Voici la construction de mon TagLib:
    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
    <?xml version="1.0" encoding="UTF-8"?>
    <taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
      <tlib-version>1.0</tlib-version>
      <short-name>taglib_library</short-name>
      <uri>/tlds/taglib_library</uri>
     
      <tag>
        <name>CalculNote</name>
        <tag-class>com.argetprod.supsta.taglib.CalculNoteTag</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
          <name>idIntervention</name>
          <rtexprvalue>true</rtexprvalue>
          <type>long</type>
        </attribute>
      </tag>
     
    </taglib>

    Et donc, pour appeler ma classe Java, je fais comme cela dans mon fichier jsp:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <tag21:CalculNote idIntervention="1"/></td>
    Il repère donc le tag qui a pour nom CalculNote, et envoi par la même occasion mon attribut de type long "idIntervention" à la classe: com.argetprod.supsta.taglib.CalculNoteTag.

    Et voici enfin le code de ma classe:
    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
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package com.argetprod.supsta.taglib;
     
    import com.argetprod.supsta.dao.DaoFactory;
    import com.argetprod.supsta.dao.InterventionDao;
    import com.argetprod.supsta.dao.NoteDao;
    import com.argetprod.supsta.entity.Intervention;
    import com.argetprod.supsta.entity.Note;
    import java.util.List;
    import javax.servlet.jsp.JspWriter;
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.tagext.JspFragment;
    import javax.servlet.jsp.tagext.SimpleTagSupport;
     
    /**
     *
     * @author Flo
     */
    public class CalculNoteTag extends SimpleTagSupport {
     
        private long idIntervention;
        private InterventionDao interventionDao;
        private NoteDao noteDao;
     
        @Override
        public void doTag() throws JspException {
     
            interventionDao = DaoFactory.getInstance().getInterventionDao();
            noteDao = DaoFactory.getInstance().getNoteDao();
            JspWriter out = getJspContext().getOut();
     
            try {
     
                long idInter = idIntervention;
                String retour = "";
                Intervention inter = interventionDao.getInterventionById(idInter);
     
                // On va lister les notes par interventions
                List<Note> notes = noteDao.rechercheNotesParIntervention(inter);
                int nbrNotes = notes.size();
                float noteSta = 0;
                float noteSlide = 0;
                float noteEvenement = 0;
     
                if (nbrNotes > 0) {
                    for (Note noteDetail : notes) {
     
                        float noteStaProvisoire = 0;
                        float noteSlideProvisoire = 0;
                        noteStaProvisoire = noteDetail.getStaConnaissances() + noteDetail.getStaPedagogie() + noteDetail.getStaReponses();
                        noteStaProvisoire = noteStaProvisoire / 3;
                        noteSlideProvisoire = noteDetail.getSlideContenu() + noteDetail.getSlideExemples() + noteDetail.getSlidePresentation();
                        noteSlideProvisoire = noteSlideProvisoire / 3;
     
                        noteSta = noteSta + noteStaProvisoire;
                        noteSlide = noteSlide + noteSlideProvisoire;
     
                    }
                    noteSta = noteSta / nbrNotes;
                    noteSlide = noteSlide / nbrNotes;
                    noteEvenement = noteSlide + noteSta;
                    noteEvenement = noteEvenement / 2;
     
                    retour = noteEvenement+"/5";
     
     
                } else {
     
                    retour = "Non noté";
     
                }
     
                out.println(retour);
     
                JspFragment f = getJspBody();
                if (f != null) {
                    f.invoke(out);
                }
     
            } catch (java.io.IOException ex) {
                throw new JspException("Error in CalculNoteTag tag", ex);
            }
        }
     
        public void setIdIntervention(long idIntervention) {
     
            this.idIntervention = idIntervention;
     
        }
    }


    Merci en tout cas pour l'aide que tu as pu m'apporter .

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

Discussions similaires

  1. Réponses: 22
    Dernier message: 27/10/2009, 10h05
  2. [JSP][Formattage] Comment faire au mieux ?
    Par ZeKiD dans le forum Servlets/JSP
    Réponses: 2
    Dernier message: 04/07/2006, 14h35
  3. Réponses: 8
    Dernier message: 07/06/2006, 22h29

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