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 :

Recuperer des informations d'une base de données et l'afficher via un <html:select>


Sujet :

Struts 1 Java

  1. #1
    Futur Membre du Club
    Inscrit en
    Janvier 2007
    Messages
    8
    Détails du profil
    Informations forums :
    Inscription : Janvier 2007
    Messages : 8
    Points : 6
    Points
    6
    Par défaut Recuperer des informations d'une base de données et l'afficher via un <html:select>
    Bonjour

    j'ai un probleme dans un formulaire,le but est que lorsque j'invoque ma page j'obtient un combo de selection avec comme contenu un resultat d'une selection d'une base de donnée
    voici mon strut-config.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
       <form-beans>
            <form-bean name="afficherInfo" type="org.apache.struts.validator.DynaValidatorForm">
                <form-property name="selectclient" type="java.lang.String"/>
            </form-bean>
        </form-beans>
        <data-sources>
            <data-source key="metal" type="org.apache.commons.dbcp.BasicDataSource">
                <set-property property="driverClass" value="com.mysql.jdbc.Driver"></set-property>
                <set-property property="url" value="jdbc:mysql://localhost:3306/metal"></set-property>
                <set-property property="username" value="root" />
                <set-property property="password" value="jemil" />
                <set-property property="minCount" value="1"></set-property>
                <set-property property="maxCount" value="5"></set-property>
            </data-source>
        </data-sources>
        <global-exceptions>
     
        </global-exceptions>
     
        <global-forwards>
            <forward name="welcome"  path="/Welcome.do"/>
        </global-forwards>
     
        <action-mappings>
     
            <action path="/afficher"  scope="request" validate="true" input="/erreurs" type="gestion.client.AjouterFournisseurAction" name="afficherInfo">
                <forward path="/reponse.do" name="reponse"/>
            </action>
            <action path="/choix" name="afficherInfo" parameter="/vues/client/client.ChoisirAfficher.jsp" type="gestion.client.DataSourceConnectionAction">
                <forward name="afficherChoixClient" path="/vues/client/client.ChoisirAfficher.jsp" redirect="true"/>
                <forward name="afficherErreurs" path="/vues/client/client.Erreurs.jsp"/>
            </action>
            <action path="/ajouter" parameter="/vues/fournisseur/fournisseur.ajouter.jsp" type="org.apache.struts.actions.ForwardAction"/>
            <action path="/Welcome" forward="/welcomeStruts.jsp"/>
        </action-mappings>
     
        <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
     
        <message-resources parameter="gestion/client/ApplicationResource"/>
    voici mon fichier web.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
     
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
        <servlet>
            <servlet-name>action</servlet-name>
            <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
            <init-param>
                <param-name>config</param-name>
                <param-value>/WEB-INF/struts-config.xml</param-value>
            </init-param>
             <init-param>
                <param-name>config/client</param-name>
                <param-value>/WEB-INF/client/struts-config.xml</param-value>
            </init-param>
            <init-param>
                <param-name>debug</param-name>
                <param-value>2</param-value>
            </init-param>
            <init-param>
                <param-name>detail</param-name>
                <param-value>2</param-value>
            </init-param>
            <load-on-startup>2</load-on-startup>
            </servlet>
        <servlet-mapping>
            <servlet-name>action</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>
    	<welcome-file>
                /vues/index.jsp
            </welcome-file>
        </welcome-file-list>
        <jsp-config>
            <taglib>
                <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
                <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
            </taglib>
            <taglib>
                <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
                <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
            </taglib>
            <taglib>
                <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
                <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
            </taglib>
            <taglib>
                <taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
                <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
            </taglib>
            <taglib>
                <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
                <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
            </taglib>
            </jsp-config>
        </web-app>
    voici la classe qui va gerer la connexion
    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
     
    /*
     * DataSourceConnectionAction.java
     *
     * Created on March 25, 2006, 4:04 PM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
     
    package gestion.client;
     
    import javax.servlet.http.*;
    import org.apache.struts.action.*;
    import java.sql.*;
    import java.util.ArrayList;
    import javax.sql.*;
     
    /**
     *
     * @author Administrator
     */
    public class DataSourceConnectionAction extends Action {
        private DataSource dataSource;
        public ArrayList listeclient = new ArrayList();
        private final static String SUCCESS = "success";
     
        public ActionForward execute(ActionMapping mapping, ActionForm  form,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
     
            HttpSession session = request.getSession();
            /** Here the method that connects to the datasource is called: */
            listeclient = getCustomers() ;
            /** Here we put the customerList in scope, so that we can use it in the JSP page: */
            if(listeclient  != null){
                session.setAttribute("affchelisteclient", listeclient);
            }
            return (mapping.findForward(SUCCESS));
        }
     
        private ArrayList getCustomers(){
            Connection conn = null;
            Statement  stmt = null;
            PreparedStatement prpStmt = null;
            ResultSet rs = null;
            StringBuffer  resultString ;
     
            try{
                /** Here 'empTable' maps to the datasource key defined in struts-config.xml: */
                dataSource = (DataSource)servlet.getServletContext().getAttribute("metal");
     
                conn = dataSource.getConnection();
                String requête = "select * from client order by marque";
                prpStmt = conn.prepareStatement(requête);
                rs = prpStmt.executeQuery();
     
                /** Here we put field 4 (the name) and field 7 (the city) in the customerList: */
                while (rs.next()) {
                    listeclient.add(new row (rs.getString(1),rs.getString(2), rs.getString(3),rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12), rs.getString(13)));
                }
                rs.close();
     
            } catch ( SQLException e ) {
                System.err.println("SQL Exception occured while accessing the table" );
                e.printStackTrace();
                return null;
     
            } catch ( Exception e ) {
                e.printStackTrace();
                return null;
            }
     
            return listeclient;
        }
    }
    et le resultat va etre aficher dans cet page qui est en meme temps la page d'acceuil du module gestion des client
    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
     
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>::>Choix d'un client</title>
        </head>
        <body>
     
        <div align="center"><h1>Choisissez un client</h1></div>
        <div align="center">
           <logic:notPresent name = "affchelisteclient">
                <h2>Data source not in scope!</h2>
            </logic:notPresent>
     
            <logic:present name = "affchelisteclient">
                <logic:empty name = "affchelisteclient">
                    <h2>Data source in scope but no data found!</h2>
                </logic:empty>
            </logic:present>
     
            <logic:present name = "affchelisteclient">
                <p>These are our users:</p>
               <html:form action="/afficher"> 
                   <table border="1">
                      <%-- <bean:write name="choix" property="matricule" /> <bean:write name="choix" property="marque" />  --%> 
                        <logic:iterate id="choix" name="affchelisteclient">
     
                            <tr>
                                <td>
                                    <html:select property="selectclient">
                                        <html:option value=""></html:option>
                                        <html:option value="matricule"><bean:write name="choix" property="matricule" /> <bean:write name="choix" property="marque" /></html:option>
     
                                    </html:select>
     
                                </td>
                            </tr>
                            </logic:iterate>
                            <tr>
                                <td><html:submit/></td>
                            </tr>
     
                    </html:form>
                </table>
           </logic:present>
        </div>
     
     
        <%--
        This example uses JSTL, uncomment the taglib directive above.
        To test, display the page like this: index.jsp?sayHello=true&name=Murphy
        --%>
        <%--
        <c:if test="${param.sayHello}">
            <!-- Let's welcome the user ${param.name} -->
            Hello ${param.name}!
        </c:if>
        --%>
     
        </body>
    </html>
    lorsque je lance tomcat et je deploi mon application il m'affiche l'erreur suivante:
    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
     
    3 juil. 2007 15:06:06 org.apache.catalina.core.ApplicationContext log
    INFO: La servlet action est marqué comme indisponible
    3 juil. 2007 15:06:06 org.apache.catalina.core.StandardContext loadOnStartup
    GRAVE: La servlet /gestion_clients a généré une exception "load()"
    javax.servlet.UnavailableException: Initializing application data source metal
            at org.apache.struts.action.ActionServlet.initModuleDataSources(ActionServlet.java:812)
            at org.apache.struts.action.ActionServlet.init(ActionServlet.java:349)
            at javax.servlet.GenericServlet.init(GenericServlet.java:211)
            at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1105)
            at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:932)
            at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3917)
            at org.apache.catalina.core.StandardContext.start(StandardContext.java:4201)
            at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:759)
            at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:739)
            at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:524)
            at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:608)
            at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:493)
            at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1204)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:585)
            at org.apache.commons.modeler.BaseModelMBean.invoke(BaseModelMBean.java:503)
            at com.sun.jmx.mbeanserver.DynamicMetaDataImpl.invoke(DynamicMetaDataImpl.java:213)
            at com.sun.jmx.mbeanserver.MetaDataImpl.invoke(MetaDataImpl.java:220)
            at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:815)
            at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:784)
            at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1377)
            at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:814)
            at org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:343)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
            at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:174)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
            at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:524)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
            at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
            at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
            at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
            at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
            at java.lang.Thread.run(Thread.java:595)
    3 juil. 2007 15:06:13 org.apache.catalina.core.StandardWrapperValve invoke
    INFO: La servlet action est actuellement indisponible
    je n'ai pas compris ce qui ce passe,pourriez vous m'indiquer le droit chemain
    Merci

  2. #2
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 4
    Points : 5
    Points
    5
    Par défaut
    Bonsoir,

    Est ce que tu as bien mis le jar du drivers de mysql dans un repertoire partagé de ton serveur (sous tomcat common/lib) ?

    Sinon la méthode getDataSource() te permet directement d'obtenir la datasource que tu as configuré.

    Fais attention à l'utilisation des classes Action. Ces classes sont poolées par les serveurs J2EE par defaut. Resultat : Une instance d'une requete d'un client peut etre réutilisée par un autre client. (notamment : les propriétés de la classe)...listeclient va posséder des doublons... au fil du temps.

  3. #3
    Futur Membre du Club
    Inscrit en
    Janvier 2007
    Messages
    8
    Détails du profil
    Informations forums :
    Inscription : Janvier 2007
    Messages : 8
    Points : 6
    Points
    6
    Par défaut
    Merci pour ta reponse

    mais comment je peux eviter ceci

Discussions similaires

  1. [Débuter] Récupérer des informations d'une base de données SQL
    Par takago dans le forum Qt Creator
    Réponses: 1
    Dernier message: 02/12/2013, 17h24
  2. [XL-2007] Sortir des informations d'une base de données en fonction de la catégorie.
    Par thibault1090 dans le forum Conception
    Réponses: 0
    Dernier message: 01/12/2012, 13h58
  3. Rechercher des informations dans une base de donnée en excel
    Par simonasjdp dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 13/07/2009, 19h10
  4. Réponses: 8
    Dernier message: 10/03/2009, 15h25
  5. Jtree avec des informations d'une base de données
    Par Lebas dans le forum Composants
    Réponses: 4
    Dernier message: 23/01/2007, 16h27

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