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 2 Java Discussion :

Création du fichier XML en utilisant un formulaire sous une application en Struts 2


Sujet :

Struts 2 Java

  1. #1
    Membre habitué
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2012
    Messages
    169
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2012
    Messages : 169
    Points : 140
    Points
    140
    Par défaut Création du fichier XML en utilisant un formulaire sous une application en Struts 2
    Bonjour,

    je veux créer un fichier xml a partir d'un formulaire jsp en utilisant struts2

    voila 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
    18
    19
    20
    21
    <%@ page contentType="text/html; charset=UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <html>
    <head>
    <title>Customer Form - Struts2 Demo | ViralPatel.net</title>
    </head>
     
    <body>
    <h2>Formulaire d'ajout de Document</h2>
     
    <s:form action="document.action" method="post" validate="true">
    	<s:textfield name="titre" key="titre" size="20" />
    	<s:textfield name="type" key="type" size="20" />
    	<s:textfield name="format" key="format" size="20" />
    	<s:textfield name="createur" key="createur" size="20" />
    	<s:textfield name="dateCreation" key="dateCreation" size="20" />
     
    	<s:submit  method="addDocument" value="Ajouter un Document" key="label.add.document" align="center" />
    </s:form>
    </body>
    </html>

    voila la classe action de ce 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
    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
    package beanAction;
     
    import java.io.FileOutputStream;
    import java.io.IOException;
     
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.transform.OutputKeys;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerConfigurationException;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
     
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.output.Format;
    import org.jdom.output.XMLOutputter;
    import org.w3c.dom.Node;
     
    import com.opensymphony.xwork2.ActionSupport;
     
    public class DocumentAction extends ActionSupport{
     
    	private static final long serialVersionUID = 1L;
     
    	private String titre;
    	private Integer type;
    	private String format;
    	private String createur;
    	private String dateCreateur;
    	static Document document;
    	 static Element racine;
     
    	public String getTitre() {
    		return titre;
    	}
     
    	public void setTitre(String titre) {
    		this.titre = titre;
    	}
     
    	public Integer getType() {
    		return type;
    	}
     
    	public void setType(Integer type) {
    		this.type = type;
    	}
     
    	public String getFormat() {
    		return format;
    	}
     
    	public void setFormat(String format) {
    		this.format = format;
    	}
     
    	public String getCreateur() {
    		return createur;
    	}
     
    	public void setCreateur(String createur) {
    		this.createur = createur;
    	}
     
    	public String getDateCreateur() {
    		return dateCreateur;
    	}
     
    	public void setDateCreateur(String dateCreateur) {
    		this.dateCreateur = dateCreateur;
    	}
     
     
     
    	public String addDocument() {
     
    		//Nous allons commencer notre arborescence en créant la racine XML
    		   //qui sera ici "personnes".
    		   Element racine = new Element("document");
     
    		   //On crée un nouveau Document JDOM basé sur la racine que l'on vient de créer
    		   document = new Document(racine);
     
     
    		      Element titre = new Element("Titre");
    		      racine.addContent(titre);
     
    		      Element type= new Element("Type");
    		      racine.addContent(type);
     
    		      Element format = new Element("Format");
    		      racine.addContent(format);
     
    		      Element createur= new Element("Createur");
    		      racine.addContent(createur);
     
    		      Element dateCreation = new Element("DateCreation");
    		      racine.addContent(dateCreation );
     
    		      try
    		      {
    		         //On utilise ici un affichage classique avec getPrettyFormat()
    		         XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
    		         //Remarquez qu'il suffit simplement de créer une instance de FileOutputStream
    		         //avec en argument le nom du fichier pour effectuer la sérialisation.
    		         sortie.output(document, new FileOutputStream("ressources/document.xml"));
    		      }
    		      catch (java.io.IOException e){}
     
     
     
    		return SUCCESS;
    	}
    }

    dans la methode addDocument j'ai mis le code de l'api jdom qui permet de créer un fichier xml dans la source (ressources/document.xml)

    le fichier xml que je souhaite créer doit etre comme ce ci :


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    <document>
    <Titre></Titre>
    <Type></Type>
    <Format></Format>
    <Createur></Createur>
    <DateCreation></DateCreation>
    </document>

    quand j'execute avec tomcat ,après je rempli le formulaire et je clique sur addDocument il m'affiche l'erreur suivante :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    type Rapport d'état
     
    message 
     
    description La ressource demandée () n'est pas disponible.

  2. #2
    Membre habitué
    Femme Profil pro
    Inscrit en
    Mai 2012
    Messages
    360
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 360
    Points : 139
    Points
    139
    Par défaut
    Est ce que le serveur tomcat est bien démarré ?
    Sinon, je pense que le problème doit venir du fichier struts.xml : l'action (et le résultat de cette dernière) ne doit pas être présente.

  3. #3
    Membre habitué
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2012
    Messages
    169
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2012
    Messages : 169
    Points : 140
    Points
    140
    Par défaut
    le serveur tomcat est bien demarer

    et voila le fichier struts.xml


    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
    <?xml version="1.0" encoding="UTF-8"?>
    	<!DOCTYPE struts PUBLIC
    	    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
     
    <struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
     
        <package name="sample" extends="struts-default">
    	        <action name="index" class="beanAction.LoginAction">
                <result name="success">./jsp/welcome.jsp</result>   
                <result name="error">./jsp/error.jsp</result>   
     
          </action>
          <action name="login">
                      <result name="success">./jsp/login.jsp</result>   
     
          </action>
     
    		 <action name="logoutAction" class="beanAction.LogoutAction">
    			<result name="login">./jsp/login.jsp</result>
    		</action>
     
    		<action name="document" method="addDocument" class="beanAction.DocumentAction">
    			<result name="error">./jsp/Document.jsp</result>
    			<result name="success">./jsp/SuccessDocument.jsp</result>
     
    		</action>
     
     
       </package>
    	</struts>

  4. #4
    Membre habitué
    Femme Profil pro
    Inscrit en
    Mai 2012
    Messages
    360
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 360
    Points : 139
    Points
    139
    Par défaut
    Je suis pas sur, mais je pense que le problème viendrait (conditionnel) de la localisation des fichiers

    regarde si au lieu de
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    <action name="document" method="addDocument" class="beanAction.DocumentAction">
    			<result name="error">./jsp/Document.jsp</result>
    			<result name="success">./jsp/SuccessDocument.jsp</result>
     
    		</action>
    tu fais
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    <action name="document" method="addDocument" class="beanAction.DocumentAction">
    			<result name="error">/jsp/Document.jsp</result>
    			<result name="success">/jsp/SuccessDocument.jsp</result>
     
    		</action>
    ou encore

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    <action name="document" method="addDocument" class="beanAction.DocumentAction">
    			<result name="error">../jsp/Document.jsp</result>
    			<result name="success">../jsp/SuccessDocument.jsp</result>
     
    		</action>
    sinon par habitude, dans les formulaire que je développe, j'ai tendance à mettre l'action à coté du nom du formulaire

    exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <form name="formAjoutCorrespondant" method="post" action="ajoutCorrespondant">

  5. #5
    Membre habitué
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2012
    Messages
    169
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2012
    Messages : 169
    Points : 140
    Points
    140
    Par défaut
    j'ai modifier ce que tu ma dit mais lorsque j'entre dans la page document.jsp

    il m'affiche ca dans la console :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    INFO: JK: ajp13 listening on /0.0.0.0:8009
    23 juil. 2012 12:39:15 org.apache.jk.server.JkMain start
    INFO: Jk running ID=0 time=0/62  config=null
    23 juil. 2012 12:39:15 org.apache.catalina.startup.Catalina start
    INFO: Server startup in 4518 ms
    23 juil. 2012 12:39:29 com.opensymphony.xwork2.util.logging.jdk.JdkLogger warn
    ATTENTION: No configuration found for the specified action: 'addDocument' in namespace: '/jsp'. Form action defaulting to 'action' attribute's literal value.
    23 juil. 2012 12:39:30 com.opensymphony.xwork2.util.logging.jdk.JdkLogger warn
    ATTENTION: No configuration found for the specified action: 'addDocument' in namespace: '/jsp'. Form action defaulting to 'action' attribute's literal value.


  6. #6
    Membre habitué
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2012
    Messages
    169
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2012
    Messages : 169
    Points : 140
    Points
    140
    Par défaut
    quand je clique sur ajouter un document il m'affiche une boite de dialogue qui met dit :

    Une erreur est survenue
    Souhaitez-vous effectuer un debogage ?
    Ligne 13
    Erreur: Objet attendu
    il me donne le choix de :
    oui ou non


    pourquoi ce message c'est quoi la cause de ca ?

  7. #7
    Membre habitué
    Femme Profil pro
    Inscrit en
    Mai 2012
    Messages
    360
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 360
    Points : 139
    Points
    139
    Par défaut
    Honnêtement, aucune idée

    En cherchant via google, j'ai trouvé ce lien :
    http://www.coderanch.com/t/484614/St...ound-specified

    Si tu tape directement dans la barre d'adresse, la localisation exact du fichier SuccessDocument.jsp, qu'est ce que çà te donne ?

  8. #8
    Membre habitué
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2012
    Messages
    169
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2012
    Messages : 169
    Points : 140
    Points
    140
    Par défaut
    quand je tape ca : http://localhost:8080/GED/jsp/SuccessDocument.jsp


    il m'affiche le contenu de la page SuccessDocument.jsp

    Document ajouté avec succès

Discussions similaires

  1. [MOSS 2007] Création automatique fichier XML pour BDC
    Par frechy dans le forum SharePoint
    Réponses: 6
    Dernier message: 06/03/2009, 11h11
  2. [DTD] Création de fichier XML
    Par ZeKiD dans le forum Format d'échange (XML, JSON...)
    Réponses: 7
    Dernier message: 20/02/2008, 15h27
  3. [DOM] création de fichier XML avec PHP
    Par cortex007 dans le forum Bibliothèques et frameworks
    Réponses: 7
    Dernier message: 09/12/2007, 11h31
  4. création de fichier xml : indentation du code généré
    Par DonKnacki dans le forum VBScript
    Réponses: 4
    Dernier message: 07/11/2007, 13h45
  5. [XSLT] Création de fichier XML dynamique avec PHP
    Par daninho dans le forum Bibliothèques et frameworks
    Réponses: 10
    Dernier message: 02/02/2006, 11h18

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