Bonjour,
J'ai un problème tout bête. Je suis une jsp, je clique sur un bouton qui génére un fichier xls via un flux.
Par contre, si il n'ya rien dans le fichier (car pas de donnéess en base), je génére une feuille excel vierge pour que ça ne planate pas. Mais je prefererai ne pas ouvrir de fichier excel et faire un forward sur ma page de sélection avec un message style "Pas de données pour ce client".
Et je bloque ...
Voici ma classe AbstractXlsAction.java
en gras, la partie qui gère s'i y a des données dans le fichier excel
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 /** * */ import java.io.IOException; import java.util.Locale; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * Action abstraite à surclasser par toutes les actions qui génèrent des * éditions Excel. * * */ public abstract class AbstractXlsAction extends AbstractAction { /* * (non-Javadoc) * * @see bnppi.ereport.struts.AbstractAction#handleContentType(javax.servlet.http.HttpServletResponse) */ protected void handleContentType(HttpServletResponse response) { response.setContentType("application/excel"); } /** * Renseigne le champ content-disposition dans l'entête de la réponse HTTP * * @param response * @param fileName */ protected void handleContentDisposition(HttpServletResponse response, String fileName) { response.setHeader("content-disposition", "attachment; filename=" + fileName); } /** * Génère l'édition et la renvoie à la sortie */ protected ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { try { User user = this.getConnectedUser(request); String uid = user.getUidUtilisateur(); Locale locale = this.getSelectedLocale(request); // remplit la table du rapport fillTableReport(form, uid, locale); // exécute l'édition jasperReports HSSFWorkbook wkb = generateWorkbook(request, uid); // affine la mise en page manageResponse(wkb,response,request,uid,mapping); } catch (IOException ioe) { throw convertSystemException(ioe); } return null; } /** * Remplit la table du rapport (proc stockée) * * @param form * @param userId */ protected abstract void fillTableReport(ActionForm form, String userId, Locale locale); /** * Génère le rapport excel (jasper reports) * * @param request * @param userId * @return * @throws IOException */ protected abstract HSSFWorkbook generateWorkbook( HttpServletRequest request, String userId) throws IOException; /** * Affine la mise en page du rapport * * @param wkb */ protected abstract void handleWorkbookLayout(HSSFWorkbook wkb); /** * Renvoie le nom du ficher à mettre dans content-disposition (pour le * téléchargement) * * @param userId * @return */ protected abstract String getFileName(String userId); /** * @param response * @param uid * @param wkb * @throws IOException */ protected void writeResponse(HttpServletResponse response, String uid, HSSFWorkbook wkb) throws IOException { handleContentType(response); handleContentDisposition(response, getFileName(uid)); wkb.write(response.getOutputStream()); } protected ActionForward findErrorFwd(ActionMapping mapping) { return mapping.findForward(FWD_ERROR_TILES); } protected void manageResponse(HSSFWorkbook wkb, HttpServletResponse response, HttpServletRequest request, String uid, ActionMapping mapping) { try { if(wkb.getNumberOfSheets()>0){ handleWorkbookLayout(wkb); // écrit le résultat à la sortie response.reset(); writeResponse(response, uid, wkb); }else{ //creation d'une feuille vierge wkb.createSheet(); handleWorkbookLayout(wkb); // écrit le résultat à la sortie response.reset(); writeResponse(response, uid, wkb); } } catch (IOException ioe) { throw convertSystemException(ioe); } } }
ma classe métier
le 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.Locale; import javax.servlet.http.HttpServletRequest; import org.apache.poi.hssf.usermodel.HSSFPrintSetup; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.struts.action.ActionForm; public class LaunchReporting13Action extends AbstractXlsAction { /** * @param wkb */ protected void handleWorkbookLayout(HSSFWorkbook wkb) { HSSFSheet sheet = wkb.getSheetAt(0); HSSFPrintSetup ps = sheet.getPrintSetup(); ps.setLandscape(true); sheet.setMargin(HSSFSheet.TopMargin, (double) .10); sheet.setMargin(HSSFSheet.BottomMargin, (double) .10); sheet.setMargin(HSSFSheet.LeftMargin, (double) .10); sheet.setMargin(HSSFSheet.RightMargin, (double) .10); } /** * @param request * @param userId * @return * @throws IOException */ protected HSSFWorkbook generateWorkbook(HttpServletRequest request, String userId) throws IOException { Locale locale = this.getLocale(request); byte[] excelFile = this.getServiceLocator() .getEReportReportingService().dataTableEreport13(userId, locale); HSSFWorkbook wkb = new HSSFWorkbook(new ByteArrayInputStream(excelFile)); return wkb; } /** * @param form * @param userId */ protected void fillTableReport(ActionForm form, String userId, Locale langue) { Criteres13Form fmrIn = (Criteres13Form) form; long id = fmrIn.getIdProprietaire(); String idProprietaire = String.valueOf(id); // remplissage de la table du rapport this.getServiceLocator().getEReportReportingService().fillTablereport13( userId, idProprietaire, langue); } protected String getFileName(String userId) { return "reporting13_" + userId + ".xls"; } }
merci ....
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444 <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <!-- ================================================ Form Bean Definitions --> <form-beans> <!-- sample form bean descriptor for an ActionForm <form-bean name="inputForm" type="app.InputForm"/> end sample --> <form-bean name="aImmeubleListForm" type="form.ImmeubleListForm"> </form-bean> <form-bean name="aSwitchForm" type="form.SwitchForm"> </form-bean> <form-bean name="aFetchImmeublesForm" type="form.FetchImmeublesForm"> </form-bean> <form-bean name="aFetchLotsForm" type=form.FetchLotsForm"> </form-bean> <form-bean name="aFetchImmeubleForm" type="form.FetchImmeubleForm"> </form-bean> <form-bean name="aLoadEditionsForm" type="form.LoadEditionsForm"> </form-bean> <form-bean name="aReporting6Form" type="form.Reporting6Form"> </form-bean> <form-bean name="aFetchLotForm" type="form.FetchLotForm"> </form-bean> <form-bean name="aCriteres6Form" type="form.Criteres6Form"> </form-bean> <form-bean name="aCriteres7Form" type="form.Criteres7Form"> </form-bean> <form-bean name="aCriteres3Form" type="form.Criteres3Form"> </form-bean> <form-bean name="aCriteres2Form" type="form.Criteres2Form"> </form-bean> <form-bean name="aCriteres13Form" type="form.Criteres13Form"> </form-bean> <form-bean name="aFetchVerifRegltForm" type="form.FetchVerifRegltForm"> </form-bean> <form-bean name="aFetchSearchOrganismeVerifForm" type="form.FetchSearchOrganismeVerifForm"> </form-bean> <form-bean name="aDeleteVerifRegltForm" type="form.DeleteVerifRegltForm"> </form-bean> <form-bean name="aFetchBudgetsForm" type="form.FetchBudgetsForm"> </form-bean> <form-bean name="aDeleteBudgetForm" type="form.DeleteBudgetForm"> </form-bean> <!-- sample form bean descriptor for a DynaActionForm <form-bean name="logonForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="username" type="java.lang.String"/> <form-property name="password" type="java.lang.String"/> </form-bean> end sample --> </form-beans> <!-- ========================================= Global Exception Definitions --> <global-exceptions> <!-- sample exception handler <exception key="expired.password" type="app.ExpiredPasswordException" path="/changePassword.jsp"/> end sample --> </global-exceptions> <!-- =========================================== Global Forward Definitions --> <global-forwards> <forward name="error" path="/jsp/error.jsp" /> <forward name="errorTiles" path="layout.error" /> <forward name="WelcomeAction" path="/WelcomeAction.do" /> <forward name="SwitchAction" path="/SwitchAction.do" /> <forward name="LogoutAction" path="/LogoutAction.do" /> <forward name="FetchImmeublesAction" path="/FetchImmeublesAction.do" /> <forward name="FetchImmeubleAction" path="/FetchImmeubleAction.do"> </forward> <forward name="FetchLotsFicheImmeubleAction" path="/FetchLotsFicheImmeubleAction.do"> </forward> <forward name="SaveImmeubleAction" path="/SaveImmeubleAction.do" /> <forward name="LoadEditionAction" path="/LoadEditionAction.do"> </forward> <forward name="LaunchReporting6" path="/LaunchReporting6.do"> </forward> <forward name="AppelCriteres2Action" path="/AppelCriteres2Action.do"> </forward> <forward name="AppelCriteres7Action" path="/AppelCriteres7Action.do"> </forward> <forward name="AppelCriteres6Action" path="/AppelCriteres6Action.do"> </forward> <forward name="AppelCriteres3Action" path="/AppelCriteres3Action.do"> </forward> <forward name="AppelCriteres13Action" path="/AppelCriteres13Action.do"> </forward> <forward name="LaunchReporting7" path="/LaunchReporting7.do"> </forward> <forward name="LaunchReporting2" path="/LaunchReporting2.do"> </forward> <forward name="LaunchReporting3" path="/LaunchReporting3.do"> </forward> <forward name="LaunchReporting3" path="/LaunchReporting13.do"> </forward> <forward name="FetchLotAction" path="/FetchLotAction.do"> </forward> <forward name="FetchJsAction" path="/FetchJsAction.do"> </forward> <forward name="FetchVerifRegltAction" path="/FetchVerifRegltAction.do"> </forward> <forward name="FetchSearchOrganismeVerifAction" path="/FetchSearchOrganismeVerifAction.do"> </forward> <forward name="AddVerifRegltAction" path="/AddVerifRegltAction.do"> </forward> <forward name="DeleteVerifRegltAction" path="/DeleteVerifRegltAction.do"> </forward> <forward name="FetchBudgetsAction" path="/FetchBudgetsAction.do"> </forward> <forward name="AddBudgetAction" path="/AddBudgetAction.do"> </forward> <forward name="DeleteBudgetAction" path="/DeleteBudgetAction.do"> </forward> </global-forwards> <!-- =========================================== Action Mapping Definitions --> <action-mappings> <!-- Default "Welcome" action --> <!-- Forwards to Welcome.jsp --> <action path="/WelcomeAction" type="org.springframework.web.struts.DelegatingActionProxy"> <forward name="success" path="layout.home" /> </action> <action path="/SwitchAction" type="org.springframework.web.struts.DelegatingActionProxy" name="aSwitchForm" scope="request" validate="true"> </action> <action path="/LogoutAction" type="org.springframework.web.struts.DelegatingActionProxy"> </action> <action path="/FetchImmeublesAction" type="org.springframework.web.struts.DelegatingActionProxy"> <forward name="success" path="/jsp/liste_immeubles.jsp"> </forward> </action> <action path="/FetchLotsAction" type="org.springframework.web.struts.DelegatingActionProxy" name="aFetchLotsForm" scope="request"> <forward name="success" path="/jsp/liste_lots.jsp"> </forward> </action> <action path="/FetchImmeubleAction" type="org.springframework.web.struts.DelegatingActionProxy" scope="request" name="aFetchImmeubleForm"> <forward name="success" path="/jsp/fiche_immeuble.jsp"> </forward> </action> <action path="/FetchLotsFicheImmeubleAction" name="aFetchLotsForm" type="org.springframework.web.struts.DelegatingActionProxy" scope="request"> <forward name="success" path="/jsp/liste_lots_immeuble.jsp"> </forward> </action> <action path="/SaveImmeubleAction" type="org.springframework.web.struts.DelegatingActionProxy" name="aFetchImmeubleForm" scope="request"> <forward name="success" path="/FetchImmeublesAction.do"> </forward> </action> <action path="/LoadEditionAction" name="aLoadEditionsForm" type="org.springframework.web.struts.DelegatingActionProxy" scope="request"> <forward name="success" path="/jsp/liste_editions.jsp"> </forward> </action> <action path="/LaunchReporting6" name="aCriteres6Form" type="org.springframework.web.struts.DelegatingActionProxy" scope="request"> </action> <action path="/LaunchReporting2" name="aCriteres2Form" type="org.springframework.web.struts.DelegatingActionProxy" scope="request"> </action> <action path="/LaunchReporting7" name="aCriteres7Form" type="org.springframework.web.struts.DelegatingActionProxy" scope="request"> </action> <action path="/LaunchReporting3" name="aCriteres3Form" type="org.springframework.web.struts.DelegatingActionProxy" scope="request"> </action> <action path="/LaunchReporting13" name="aCriteres13Form" type="org.springframework.web.struts.DelegatingActionProxy" scope="request"> </action> <action path="/FetchLotAction" type="org.springframework.web.struts.DelegatingActionProxy" scope="request" name="aFetchLotForm"> <forward name="success" path="/jsp/fiche_lot.jsp"></forward> </action> <action path="/SaveLotAction" type="org.springframework.web.struts.DelegatingActionProxy" name="aFetchLotForm" scope="request"> <forward name="success" path="/FetchImmeubleAction.do"> </forward> </action> <action path="/AppelCriteres6Action" type="org.springframework.web.struts.DelegatingActionProxy" name="aCriteres6Form" scope="request"> <forward name="success" path="/jsp/critere_selection_ereport6.jsp"> </forward> </action> <action path="/AppelCriteres7Action" type="org.springframework.web.struts.DelegatingActionProxy" name="aCriteres7Form" scope="request"> <forward name="success" path="/jsp/critere_selection_ereport7.jsp"> </forward> </action> <action path="/AppelCriteres2Action" type="org.springframework.web.struts.DelegatingActionProxy" name="aCriteres2Form" scope="request"> <forward name="success" path="/jsp/critere_selection_ereport2.jsp"> </forward> </action> <action path="/AppelCriteres3Action" type="org.springframework.web.struts.DelegatingActionProxy" name="aCriteres3Form" scope="request"> <forward name="success" path="/jsp/critere_selection_ereport3.jsp"> </forward> </action> <action path="/AppelCriteres13Action" type="org.springframework.web.struts.DelegatingActionProxy" name="aCriteres13Form" scope="request"> <forward name="success" path="/jsp/critere_selection_ereport13.jsp"> </forward> </action> <action path="/FetchJsAction" forward="/jsp/js.jsp"></action> <action path="/FetchVerifRegltAction" type="org.springframework.web.struts.DelegatingActionProxy" name="aFetchVerifRegltForm" scope="request"> <forward name="success" path="/jsp/fiche_verif_reglt.jsp"> </forward> </action> <action path="/FetchSearchOrganismeVerifAction" type="org.springframework.web.struts.DelegatingActionProxy" name="aFetchSearchOrganismeVerifForm" scope="request"> <forward name="success" path="/jsp/dialog_search_organisme_verif.jsp"> </forward> </action> <action path="/AddVerifRegltAction" type="org.springframework.web.struts.DelegatingActionProxy" name="aFetchVerifRegltForm" scope="request"> <forward name="success" path="/FetchVerifRegltAction.do"> </forward> </action> <action path="/DeleteVerifRegltAction" type="org.springframework.web.struts.DelegatingActionProxy" name="aDeleteVerifRegltForm" scope="request"> <forward name="success" path="/FetchVerifRegltAction.do"> </forward> </action> <action path="/FetchBudgetsAction" type="org.springframework.web.struts.DelegatingActionProxy" name="aFetchBudgetsForm" scope="request"> <forward name="success" path="/jsp/fiche_budget.jsp"> </forward> </action> <action path="/AddBudgetAction" type="org.springframework.web.struts.DelegatingActionProxy" name="aFetchBudgetsForm" scope="request"> <forward name="success" path="/FetchBudgetsAction.do"> </forward> </action> <action path="/DeleteBudgetAction" type="org.springframework.web.struts.DelegatingActionProxy" name="aDeleteBudgetForm" scope="request"> <forward name="success" path="/FetchBudgetsAction.do"> </forward> </action> <!-- sample input and input submit actions <action path="/Input" type="org.apache.struts.actions.ForwardAction" parameter="/pages/Input.jsp"/> <action path="/InputSubmit" type="app.InputAction" name="inputForm" scope="request" validate="true" input="/pages/Input.jsp"/> <action path="/edit*" type="app.Edit{1}Action" name="inputForm" scope="request" validate="true" input="/pages/Edit{1}.jsp"/> end samples --> </action-mappings> <controller> <set-property property="processorClass" value="org.springframework.web.struts.DelegatingTilesRequestProcessor" /> </controller> <!-- ======================================== Message Resources Definitions --> <message-resources parameter="resources/MessageResources" /> <message-resources parameter="typeLot" factory="struts.TypeLotMessageResourcesFactory" key="typeLot" /> <message-resources parameter="typeContrat" factory="struts.TypeContratMessageResourcesFactory" key="typeContrat" /> <message-resources parameter="typeVerif" factory="bnppi.ereport.struts.TypeVerifMessageResourcesFactory" key="typeVerif" /> <message-resources parameter="natureImmeuble" factory="struts.NatureImmeubleMessageResourcesFactory" key="natureImmeuble" /> <message-resources parameter="typeImmeuble" factory="struts.TypeImmeubleMessageResourcesFactory" key="typeImmeuble" /> <message-resources parameter="typeOccupation" factory="struts.TypeOccupationMessageResourcesFactory" key="typeOccupation" /> <message-resources parameter="affectationJuridique" factory=".struts.AffectationJuridiqueMessageResourcesFactory" key="affectationJuridique" /> <message-resources parameter="typeCharge" factory="bnppi.ereport.struts.TypeChargeMessageResourcesFactory" key="typeCharge" /> <!-- =============================================== Plug Ins Configuration --> <!-- ======================================================= Tiles plugin --> <!-- This plugin initialize Tiles definition factory. This later can takes some parameters explained here after. The plugin first read parameters from web.xml, thenoverload them with parameters defined here. All parameters are optional. The plugin should be declared in each struts-config file. - definitions-config: (optional) Specify configuration file names. There can be several comma separated file names (default: ?? ) - moduleAware: (optional - struts1.1) Specify if the Tiles definition factory is module aware. If true (default), there will be one factory for each Struts module. If false, there will be one common factory for all module. In this later case, it is still needed to declare one plugin per module. The factory will be initialized with parameters found in the first initialized plugin (generally the one associated with the default module). true : One factory per module. (default) false : one single shared factory for all modules - definitions-parser-validate: (optional) Specify if xml parser should validate the Tiles configuration file. true : validate. DTD should be specified in file header (default) false : no validation Paths found in Tiles definitions are relative to the main context. To use this plugin, download and add the Tiles jar to your WEB-INF/lib directory then uncomment the plugin definition below. --> <plug-in className="org.apache.struts.tiles.TilesPlugin"> <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" /> <set-property property="definitions-parser-validate" value="true" /> </plug-in> <!-- =================================================== Validator plugin --> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/org/apache/struts/validator/validator-rules.xml, /WEB-INF/validation.xml" /> </plug-in> <!-- =================================================== Spring 2 plugin --> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/action-servlet.xml, /WEB-INF/applicationContext.xml" /> </plug-in> </struts-config>
Partager