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 :

Problème de recupération des données du formulaire lors du submit avec Struts2


Sujet :

Struts 1 Java

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Inscrit en
    Septembre 2013
    Messages
    34
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Septembre 2013
    Messages : 34
    Points : 29
    Points
    29
    Par défaut Problème de recupération des données du formulaire lors du submit avec Struts2
    Bonjour,

    J'ai une application Struts2 dans laquelle j'ai des formulaires de saisies (jusque la rien d'exceptionnel...). Lorsque je soumet mon formulaire les données saisies dedans et mappées dans l'ActionSupport ne sont pas mises à jours. Elles sont toujours à null...je ne saisie pas trop pourquoi. Vous trouverez ci-dessous le code source :

    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
    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
     
    <?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.devMode" value="false" />
    	<constant name="struts.action.extension" value="do" />
    	<constant name="struts.objectFactory" value="spring" />
    	<constant name="struts.objectFactory.spring.autoWire" value="name" />
    	<constant name="struts.i18n.encoding" value="ISO-8859-1" />
    	<constant name="struts.i18n.reload" value="false" />
    	<constant name="struts.configuration.xml.reload" value="false" />
    	<constant name="struts.locale" value="fr" />
    	<constant name="struts.multipart.maxSize" value="100000000000" />
     
    	<package name="default" extends="tiles-default" namespace="/">
     
    		<interceptors>
     
    			<interceptor name="params-filter"
    				class="com.opensymphony.xwork2.interceptor.ParameterFilterInterceptor" />
    			<interceptor-stack name="defaultStack">
    				<interceptor-ref name="exception" />
    				<interceptor-ref name="alias" />
    				<interceptor-ref name="servlet-config" />
    				<interceptor-ref name="i18n" />
    				<interceptor-ref name="chain" />
    				<interceptor-ref name="model-driven" />
    				<interceptor-ref name="fileUpload">
    					<param name="maximumSize">11204928</param>
    				</interceptor-ref>
    				<interceptor-ref name="static-params" />
    				<interceptor-ref name="conversionError" />
    				<interceptor-ref name="params" />
    				<interceptor-ref name="prepare" />
    				<interceptor-ref name="validation" />
    				<interceptor-ref name="workflow" />
    				<interceptor-ref name="userAware" />
    			</interceptor-stack>
     
    		</interceptors>
     
    		<default-interceptor-ref name="defaultStack" />
     
    		<global-results>
    			<result name="technicalError" type="chain">
    				errorAction
    			</result>
    			<result name="sessionInvalidError" type="tiles">
    				sessionInvalid
    			</result>
    			<result name="blank" type="tiles">blank</result>
    		</global-results>
     
    		<global-exception-mappings>
    			<exception-mapping exception="java.lang.Exception"
    				result="technicalError" />
    			<exception-mapping
    				exception="com.omb.service.exception.UserSessionInvalidException"
    				result="sessionInvalidError" />
     
    		</global-exception-mappings>
     
    		<package name="omb" extends="default" namespace="/omb">
    			<action name="*OMBAction" class="com.omb.MyAction" method="doMyAction">
    			<result name="ok" type="tiles">maPage</result>
    		</action>
    	</package>
    </struts>
    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
     
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <%@ taglib uri="http://displaytag.sf.net/el" prefix="display"%>
    <link rel="stylesheet" href="<s:url value="/windowfiles/dhtmlwindow.css"/>" type="text/css" />
     
     
    <script language="javascript"><!--
    mes scripts
    --></script>
    <s:form action="valoriseHiddenOMBAction.do" theme="simple" id="hiddenForm">
    	<s:hidden name="champ1" value="%{champ1}"></s:hidden>
    	<s:hidden name="champ2" value="%{champ2}"></s:hidden>
    </s:form>
     
    <table height="100%" width="100%" border="0">
    	<s:form action="doOMBAction.do" name="ombForm" id="idOmbForm" theme="simple">
     
    		<s:hidden name="user.userId" value="%{user.userId}"></s:hidden>
    		<s:hidden name="period.periodId" value="%{period.periodId}"></s:hidden>
     
    		<tr>
    			<td>
    				<input  type="button"  value="download" onclick = "javascript:location.href='afficheOMBReport.do';"/>
    			</td>
    		</tr>
    		<tr>
    			<td width="10%"><label ><s:text name="champ1.label"></s:text></label></td>
    			<td width="4%">&nbsp;</td>
    			<td width="10%"><label ><s:text name="champ2.label"></s:text></label></td>
    		</tr>
    		<tr>
    			<td>
    				<s:select cssStyle="width:200" list="listChamp1" id="idChamp1"
    					value="champ1.identifiant" listKey="identifiant" listValue="label"/>
    			</td>
    			<td>&nbsp;</td>
    			<td>
    				<s:select cssStyle="width:200" list="listChamp2" id="idChamp2"
    					value="champ2.identifiant" listKey="identifiant" listValue="label"/>
    			</td>
    		</tr>
    		<tr>
    			<td>
    				<input  type="button"  value="Afficher les valeurs" onclick = "javascript:location.href='afficheValuesOMBAction.do';"/>
    			</td>
    		</tr>
    	</s:form>
    </table>
    Classe Champ
    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
     
    /**
     * 
     */
    package com.omb;
     
    public class Champ {
     
        private Integer identifiant;
     
        private String label;
     
        public Champ(Integer identifiant, String label) {
            super();
            this.identifiant = identifiant;
            this.label = label;
        }
     
        /**
         * @return the identifiant
         */
        public Integer getIdentifiant() {
            return identifiant;
        }
     
        /**
         * @param identifiant the identifiant to set
         */
        public void setIdentifiant(Integer identifiant) {
            this.identifiant = identifiant;
        }
     
        /**
         * @return the label
         */
        public String getLabel() {
            return label;
        }
     
        /**
         * @param label the label to set
         */
        public void setLabel(String label) {
            this.label = label;
        }
     
    }
    Classe 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
    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
     
    /**
     * 
     */
    package com.omb;
     
    import java.util.ArrayList;
    import java.util.List;
     
    import com.opensymphony.xwork2.ActionSupport;
     
    /**
     * 
     * 
     */
    public class MyAction extends ActionSupport {
     
        private Champ champ1;
     
        private Champ champ2;
     
        private List<Champ> listChamp1;
     
        private List<Champ> listChamp2;
     
        public String execute() throws Exception {
     
            listChamp1 = new ArrayList<Champ>();
            listChamp1.add(new Champ(1, "valeur1"));
            listChamp1.add(new Champ(2, "valeur2"));
     
            listChamp2 = new ArrayList<Champ>();
            listChamp2.add(new Champ(3, "valeur3"));
            listChamp2.add(new Champ(4, "valeur4"));
     
            return SUCCESS;
        }
     
        public String doMyAction() {
     
            System.out.println("ID Champ 1 = " + champ1.getIdentifiant());
            System.out.println("Label Champ 1 = " + champ1.getLabel());
            System.out.println("ID Champ 2 = " + champ2.getIdentifiant());
            System.out.println("Label Champ 2 = " + champ2.getLabel());
     
            return "ok";
        }
     
        /**
         * @return the champ1
         */
        public Champ getChamp1() {
            return champ1;
        }
     
        /**
         * @param champ1 the champ1 to set
         */
        public void setChamp1(Champ champ1) {
            this.champ1 = champ1;
        }
     
        /**
         * @return the champ2
         */
        public Champ getChamp2() {
            return champ2;
        }
     
        /**
         * @param champ2 the champ2 to set
         */
        public void setChamp2(Champ champ2) {
            this.champ2 = champ2;
        }
     
        /**
         * @return the listChamp1
         */
        public List<Champ> getListChamp1() {
            return listChamp1;
        }
     
        /**
         * @param listChamp1 the listChamp1 to set
         */
        public void setListChamp1(List<Champ> listChamp1) {
            this.listChamp1 = listChamp1;
        }
     
        /**
         * @return the listChamp2
         */
        public List<Champ> getListChamp2() {
            return listChamp2;
        }
     
        /**
         * @param listChamp2 the listChamp2 to set
         */
        public void setListChamp2(List<Champ> listChamp2) {
            this.listChamp2 = listChamp2;
        }
     
    }
    Lorsque la méthode doMyAction est appelé les résultat est toujours null pour les champs que j'affiche même si j'ai saisie de la données dedans.

  2. #2
    Candidat au Club
    Profil pro
    Inscrit en
    Décembre 2012
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2012
    Messages : 3
    Points : 3
    Points
    3
    Par défaut
    il me semble que lorsque tu envoye des données a travers un formulaire, ces données sont de type String.

    Donc ton action attend des valeur de type String également.
    Or dans ton cas tu as déclare tes attributs de type Champ!?

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Inscrit en
    Septembre 2013
    Messages
    34
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Septembre 2013
    Messages : 34
    Points : 29
    Points
    29
    Par défaut
    Je reviens sur cet ancien post que j'avais fais lors de mes debut sur Struts2 en le relisant je sais ou est l'erreur. En fait dans le select j'ai mappé les champs sur l'attribut value qui sert à restituer la valeur lors de l'affichage de l'ecran. Pour valoriser la sélection il faut ajouter l'attribut name. Ci-dessous la version corrigé.

    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
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <%@ taglib uri="http://displaytag.sf.net/el" prefix="display"%>
    <link rel="stylesheet" href="<s:url value="/windowfiles/dhtmlwindow.css"/>" type="text/css" />
     
     
    <script language="javascript"><!--
    mes scripts
    --></script>
    <s:form action="valoriseHiddenOMBAction.do" theme="simple" id="hiddenForm">
    	<s:hidden name="champ1" value="%{champ1}"></s:hidden>
    	<s:hidden name="champ2" value="%{champ2}"></s:hidden>
    </s:form>
     
    <table height="100%" width="100%" border="0">
    	<s:form action="doOMBAction.do" name="ombForm" id="idOmbForm" theme="simple">
     
    		<tr>
    			<td>
    				<input  type="button"  value="download" onclick = "javascript:location.href='afficheOMBReport.do';"/>
    			</td>
    		</tr>
    		<tr>
    			<td width="10%"><label ><s:text name="label1"></s:text></label></td>
    			<td width="4%">&nbsp;</td>
    			<td width="10%"><label ><s:text name="label2"></s:text></label></td>
    		</tr>
    		<tr>
    			<td>
    				<s:select cssStyle="width:200" list="listChamp1" id="idChamp1"
    					value="identifiant1" name="identifiant1" listKey="identifiant" listValue="label"/>
    			</td>
    			<td>&nbsp;</td>
    			<td>
    				<s:select cssStyle="width:200" list="listChamp2" id="idChamp2"
    					value="identifiant2" name="identifiant2" listKey="identifiant" listValue="label"/>
    			</td>
    		</tr>
    		<tr>
    			<td>
    				<input  type="button"  value="Afficher les valeurs" onclick = "javascript:location.href='afficheValuesOMBAction.do';"/>
    			</td>
    		</tr>
    	</s:form>
    </table>
    Java

    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
    /**
     * 
     */
    package com.omb;
     
    import java.util.ArrayList;
    import java.util.List;
     
    import com.opensymphony.xwork2.ActionSupport;
     
    /**
     * 
     * 
     */
    public class MyAction extends ActionSupport {
     
        private Integer identifiant1;
     
        private Integer identifiant2;
     
        private List<Champ> listChamp1;
     
        private List<Champ> listChamp2;
     
        private String label1;
     
        private String label2;
     
       private Service monService;
     
        public String execute() throws Exception {
     
            listChamp1 = new ArrayList<Champ>();
            listChamp1.add(new Champ(1, "valeur1"));
            listChamp1.add(new Champ(2, "valeur2"));
     
            listChamp2 = new ArrayList<Champ>();
            listChamp2.add(new Champ(3, "valeur3"));
            listChamp2.add(new Champ(4, "valeur4"));
     
            return SUCCESS;
        }
     
        public String doMyAction() {
     
            Champ champ1 = monService.chargerChamp(identifiant1);
            Champ champ2 = monService.chargerChamp(identifiant2);
     
            System.out.println("ID Champ 1 = " + champ1.getIdentifiant());
            System.out.println("Label Champ 1 = " + champ1.getLabel());
            System.out.println("ID Champ 2 = " + champ2.getIdentifiant());
            System.out.println("Label Champ 2 = " + champ2.getLabel());
     
            return "ok";
        }
     
        public Integer getIdentifiant1() {
            return identifiant1;
        }
     
        public void setIdentifiant1(Integer identifiant1) {
            this.identifiant1 = identifiant1;
        }
     
        public Champ getIdentifiant2() {
            return identfiant2;
        }
     
        public void setIdentifiant2(Integer identfiant2) {
            this.identfiant2= identfiant2;
        }
     
        public List<Champ> getListChamp1() {
            return listChamp1;
        }
     
        public void setListChamp1(List<Champ> listChamp1) {
            this.listChamp1 = listChamp1;
        }
     
        public List<Champ> getListChamp2() {
            return listChamp2;
        }
     
        public void setListChamp2(List<Champ> listChamp2) {
            this.listChamp2 = listChamp2;
        }
     
        public String getLabel1() {
            return label1;
        }
     
        public void setLabel1(String label1) {
            this.label1 = label1;
        }
     
        public String getLabel2() {
            return label2;
        }
     
        public void setLabel2(String label2) {
            this.label2 = label2;
        }
     
        public String getMonService() {
            return monService;
        }
     
        public void setMonService(String monService) {
            this.monService= monService;
        }
     
    }

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

Discussions similaires

  1. Réponses: 1
    Dernier message: 01/08/2013, 18h23
  2. [MySQL] message d'erreur lors de l'envois des données du formulaires
    Par paolo129 dans le forum PHP & Base de données
    Réponses: 1
    Dernier message: 16/12/2008, 17h14
  3. récuperer des données de formulaire sans bouton submit
    Par bachboucha dans le forum Langage
    Réponses: 1
    Dernier message: 29/10/2008, 15h13
  4. Réponses: 19
    Dernier message: 16/01/2008, 17h41
  5. Réponses: 8
    Dernier message: 16/06/2006, 00h57

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