Bonjour tout le monde je sais que d'aucun vont me dire que c'est une question deja posé est resolu mais j'ai un bleme j'ai mis tous les *.jar dans mon appli comme commons-dbcp.jar et common-pool.jar struts_legacy.jar etc..; dans mes lib
je travail avec tomcat
dans mon fichier struts-config.xml
j'ai ceci
mon fichier mydatasourceSuccessful.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 <data-sources > <data-source type="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" key="org.apache.struts.action.DATA_SOURCE"> <set-property property="minCount" value="10" /> <set-property property="password" value="teste" /> <set-property property="maxCount" value="500" /> <set-property property="user" value="root" /> <set-property property="driverClass" value="com.mysql.jdbc.Driver" /> <set-property property="description" value="Data" /> <set-property property="url" value="jdbc:mysql://localhost:3306/testedb" /> <set-property property="autoCommit" value="true" /> <set-property property="readOnly" value="false" /> <set-property property="loginTimeout" value="60" /> </data-source> </data-sources>
mon bean 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 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <html:html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title> JSP LISTE </title> </head> <body> <h1> Bienvenue</h1> <logic:notPresent name ="allMyCustomers"> <h2> Datasource not in scope </h2> </logic:notPresent> <logic:present name ="allMyCustomers"> <logic:empty name="allMyCustomers"> <h2> Datasource in scope but no data found</h2> </logic:empty> </logic:present> <logic:present name="allMycustomers"> <p>liste des utilisateurs</p> <table border="1"> <tbody> <logic:iterate id="customer" name="allMyCustomers"> <tr> <td><bean:write name="customer" property="id" /></td> <td><bean:write name="customer" property="login" /></td> </tr> </logic:iterate> </tbody> </table> </logic:present> <p> <html:link action="/logout" linkName="Log me out">Logout</html:link> </body> </html:html>
mon fichier DatasourceConnectionAction.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 package com.spriveseng.seysoo.asm.struts.action; public class row { /** Creates a new instance of Row */ private String id; private String login; /** Creates a new instance of Row */ public row(String id, String login) { this.id = id; this.login = login; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } }
puis pour tester tout ceci je fais le mapping suivant
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 package com.spriveseng.seysoo.asm.struts.action; import javax.servlet.http.*; import org.apache.struts.action.*; import java.sql.*; import java.util.ArrayList; import javax.sql.*; import org.apache.struts.Globals; import javax.servlet.http.HttpServletRequest; public class DatasourceConnectionAction extends Action { private DataSource dataSource; public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DataSource dataSource; ArrayList customerList = new ArrayList(); final String SUCCESS = "success"; HttpSession session = request.getSession(); /** Ici, la méthode qui fait la connection à la DataSource est appelée: */ customerList = getCustomers() ; /** Ici, nous mettons le customerList dans le scope, pour pouvoir l'utiliser dans la page JSP: */ if(customerList != null){ session.setAttribute("allMyCustomers" , customerList); } return (mapping.findForward(SUCCESS)); } private ArrayList getCustomers(){ Connection conn = null; Statement stmt = null; PreparedStatement prpStmt = null; ResultSet rs = null; StringBuffer resultString ; ArrayList<row> customerList = null; try{ /** Ici, 'empTable' est associé à la clef de la DataSource dans struts-config.xml: */ DataSource dataSource = (DataSource)servlet.getServletContext() .getAttribute(Globals.DATA_SOURCE_KEY); conn = dataSource.getConnection(); String sqlQuery = "select id,login from acces order by login"; prpStmt = conn.prepareStatement(sqlQuery); rs = prpStmt.executeQuery(); /** Ici, nous avons mis le champs 4 (le nom) et le champs 7 (la ville) dans la customerList: */ while (rs.next()) { customerList.add(new row(rs.getString(1), rs.getString(2))); } 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 customerList; } }
j obtiens ceci
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 <action path="/MyDS" type="com.spriveseng.seysoo.asm.struts.action.DatasourceConnectionAction"> <forward name="success" path="/AccueilJSP/mydatasourceSuccessful.jsp"/> </action>
comme resultat
si quelqu un pourrai m'aiderDatasource not in scope
merci d'avance
Partager