Data source utilisable dans mes classe JAVA
Bonjour à tous;
Je veux être plus proche d'une architecture MVC dans mes JSP page donc je veux faire le contenu suivant directement dans du .java Je n'arrive pas à trouver la fonçons de faire avec l'abstraction fournis avec ma DataSource?
Ma data Source: (context.xml)
Code:
1 2 3 4 5 6 7 8 9 10
|
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/TestDB" docBase="TestDB"
debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000" username="bilred"
password="*****" driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/DB"/>
</Context> |
aussi mon WEB-INF/web.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>TestDB</display-name>
<welcome-file-list>
<welcome-file>TestDB.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app> |
Ma .jap à modifié:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<sql:query var="rs" dataSource="jdbc/TestDB">
select id, foo, bar from data
</sql:query>
<html>
<head>
<title>Test with DB</title>
</head>
<body>
<h2>Results</h2>
<c:forEach var="row" items="${rs.rows}">
Foo ${row.foo}<br />
Bar ${row.bar}<br />
</c:forEach>
</body>
</html> |
Merci vous suggestions
NB: j'utilise que du Tomcat.