Bonjour,
je suis débutant en developpement struts et hibernate, et j'essaye d'afficher quelques colonnes d'une table de ma base de données dans une JSP, en suivant ce tuto: http://viralpatel.net/blogs/2009/06/...ag-struts.html.
Mais j'ai pas réussi ma tentative, car j'ai cette exception que je ne sais pas comment la resoudre:
org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NullPointerException
Voici mon code, pour mieux comprendre le problème:
le bean FArticle.java
le FArticleDAO.java généré par hibernate:
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 package com.hibernate.application2; import java.math.BigDecimal; /** * FArticle entity. @author MyEclipse Persistence Tools */ public class FArticle extends AbstractFArticle implements java.io.Serializable { // Constructors /** default constructor */ public FArticle() { } /** minimal constructor */ public FArticle(FArticleId id, String desArt, String codFam) { super(id, desArt, codFam); } /** full constructor */ public FArticle(FArticleId id, String desArt, String codMes, String codFam, Double priRevien, Integer priVente, Integer mntConsom, Integer mntOuvre, Integer priPub, Byte codRayon, String typArt, BigDecimal txTva, String actif, String ean) { super(id, desArt, codMes, codFam, priRevien, priVente, mntConsom, mntOuvre, priPub, codRayon, typArt, txTva, actif, ean); } }
Le form bean ListeArticlesForm.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 public class FArticleDAO extends BaseHibernateDAO { private static final Log log = LogFactory.getLog(FArticleDAO.class); // property constants public static final String DES_ART = "desArt"; public static final String COD_MES = "codMes"; public static final String COD_FAM = "codFam"; public static final String PRI_REVIEN = "priRevien"; public static final String PRI_VENTE = "priVente"; public static final String MNT_CONSOM = "mntConsom"; public static final String MNT_OUVRE = "mntOuvre"; public static final String PRI_PUB = "priPub"; public static final String COD_RAYON = "codRayon"; public static final String TYP_ART = "typArt"; public static final String ACTIF = "actif"; public static final String EAN = "ean"; public void save(FArticle transientInstance) { log.debug("saving FArticle instance"); try { getSession().save(transientInstance); log.debug("save successful"); } catch (RuntimeException re) { log.error("save failed", re); throw re; } } public void delete(FArticle persistentInstance) { log.debug("deleting FArticle instance"); try { getSession().delete(persistentInstance); log.debug("delete successful"); } catch (RuntimeException re) { log.error("delete failed", re); throw re; } } public FArticle findById(com.hibernate.application2.FArticleId id) { log.debug("getting FArticle instance with id: " + id); try { FArticle instance = (FArticle) getSession().get( "com.hibernate.application2.FArticle", id); return instance; } catch (RuntimeException re) { log.error("get failed", re); throw re; } } public List findByExample(FArticle instance) { log.debug("finding FArticle instance by example"); try { List results = getSession().createCriteria( "com.hibernate.application2.FArticle").add( Example.create(instance)).list(); log.debug("find by example successful, result size: " + results.size()); return results; } catch (RuntimeException re) { log.error("find by example failed", re); throw re; } } public List findByProperty(String propertyName, Object value) { log.debug("finding FArticle instance with property: " + propertyName + ", value: " + value); try { String queryString = "from FArticle as model where model." + propertyName + "= ?"; Query queryObject = getSession().createQuery(queryString); queryObject.setParameter(0, value); return queryObject.list(); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } public List findByDesArt(Object desArt) { return findByProperty(DES_ART, desArt); } public List findByCodMes(Object codMes) { return findByProperty(COD_MES, codMes); } public List findByCodFam(Object codFam) { return findByProperty(COD_FAM, codFam); } public List findByPriRevien(Object priRevien) { return findByProperty(PRI_REVIEN, priRevien); } public List findByPriVente(Object priVente) { return findByProperty(PRI_VENTE, priVente); } public List findByMntConsom(Object mntConsom) { return findByProperty(MNT_CONSOM, mntConsom); } public List findByMntOuvre(Object mntOuvre) { return findByProperty(MNT_OUVRE, mntOuvre); } public List findByPriPub(Object priPub) { return findByProperty(PRI_PUB, priPub); } public List findByCodRayon(Object codRayon) { return findByProperty(COD_RAYON, codRayon); } public List findByTypArt(Object typArt) { return findByProperty(TYP_ART, typArt); } public List findByActif(Object actif) { return findByProperty(ACTIF, actif); } public List findByEan(Object ean) { return findByProperty(EAN, ean); } public List findAll() { log.debug("finding all FArticle instances"); try { String queryString = "from FArticle"; Query queryObject = getSession().createQuery(queryString); return queryObject.list(); } catch (RuntimeException re) { log.error("find all failed", re); throw re; } } public FArticle merge(FArticle detachedInstance) { log.debug("merging FArticle instance"); try { FArticle result = (FArticle) getSession().merge(detachedInstance); log.debug("merge successful"); return result; } catch (RuntimeException re) { log.error("merge failed", re); throw re; } } public void attachDirty(FArticle instance) { log.debug("attaching dirty FArticle instance"); try { getSession().saveOrUpdate(instance); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public void attachClean(FArticle instance) { log.debug("attaching clean FArticle instance"); try { getSession().lock(instance, LockMode.NONE); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } }
L'action ListeArticlesAction.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 public class ListeArticlesForm extends ActionForm { /* * Generated Methods */ /** * */ private static final long serialVersionUID = 1L; /** * Method reset * @param mapping * @param request */ private List<FArticle> articles; public List<FArticle> getArticles() { return articles; } public void setArticles(List<FArticle> article) { this.articles = article; } }
les JSPs:
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 public class ListeArticlesAction extends Action { private static final FArticleDAO FArticleDAO = null; @SuppressWarnings("unchecked") public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { ListeArticlesForm listeArticlesForm = (ListeArticlesForm) form; FArticleDAO articledao = FArticleDAO; listeArticlesForm.setArticles(articledao.findAll()); return mapping.findForward("showList"); } }
index.jsp
listeArticles.jsp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 <%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <jsp:forward page="MylisteArticles.do"/>
struts-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
40
41
42 <%@ page language="java" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <%@ taglib uri="http://displaytag.sf.net" prefix="display" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html:html lang="true"> <head> <html:base /> <title>listeArticle.jsp</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h2>Liste des articles existants:</h2><br> <display:table export="true" name="sessionScope.ListeArticlesForm.articles" requestURI="MylisteArticles" pagesize="20" > <display:column property="id" title="Code" /> <display:column property="desArt" title="Designation" /> <display:column property="priVente" title="P.U" /> <display:column property="typArt" title="Type Article" /> </display:table> </body> </html:html>
ET le 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 <form-beans > <form-bean name="ListeArticlesForm" type="com.ensi.struts.form.ListeArticlesForm" /> </form-beans> <action attribute="ListeArticlesForm" name="ListeArticlesForm" path="/MylisteArticles" scope="session" type="com.ensi.struts.action.ListeArticlesAction"> <forward name="showList" path="/listeArticles.jsp" /> </action>
je vous remercie pour votre entraide
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 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" 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>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>./index.jsp</welcome-file> </welcome-file-list> </web-app>
Partager