Bonjour,
Voici mon petit problème journalier de débutant.
Dans l'exemple, on se logue aprés avoir donné un login et un pass bidon, et on arrive sur la page des articles
Le problème est qu'il manque une classe form.
J'ai essayé de la faire tout seul et devinez quoi...Et bien débutant comme je suis j'y arrive pas.
Le problème est que dans sa classe form il utilise un objet qu'il a créé, et je ne sais pas trop comment implémenter les méthodes set et get qui vont avec.
J'ai donc l'erreur suivant: Cannot find bean ChooseForm in any scope
Voici les sources:
les java bean qui servent de source de données
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 public class Article { // Un bon vieux Java Bean bien classique private int m_IdArticle; private String m_Marque; private String m_Designation; private double m_Price; public int getIdArticle() { return this.m_IdArticle; } public void setIdArticle(int idArticle) throws Exception { if (idArticle<0) throw new Exception("IdArticle must be positive"); this.m_IdArticle = idArticle; } public String getMarque() { return this.m_Marque; } public void setMarque(String marque) { this.m_Marque = marque.toUpperCase(); } public String getDesignation() { return this.m_Designation; } public void setDesignation(String designation) { this.m_Designation = designation.toLowerCase(); } public double getPrice() { return this.m_Price; } public void setPrice(double price) throws Exception { if (price<0) throw new Exception("Price must be positive"); this.m_Price = price; } public Article() throws Exception { this(0, "Inconnue", "???", 1); } public Article(int id, String marque, String designation, double price) throws Exception { this.setIdArticle(id); this.setMarque(marque); this.setDesignation(designation); this.setPrice(price); } public String toString() { return this.m_IdArticle + ": article " + this.m_Designation + " de marque " + this.m_Marque + " et de prix " + this.m_Price + " Euros"; } }///////////////////// la class chooseAction /////////////////////
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 // Cette implémentation de catalogue d'articles est très simpliste, // je vous l'accorde. Mais le but consiste à vous montrer certains concepts // fondamentaux de Struts. Je ne préfère donc pas alourdir le reste du code !!! public class Catalog { public static Article [] m_Articles = new Article[4]; static { try { m_Articles[0] = new Article(0, "Byque", "Stylo bleu", 1); m_Articles[1] = new Article(1, "FoncéFontaine", "Ramette papier", 5); m_Articles[2] = new Article(2, "Harry beau", "bonbon", 0.2); m_Articles[3] = new Article(3, "Bidule", "Equerre", 3); } catch(Exception e) { System.err.println("Ouille !"); } } }
///////////////////// la class chooseform ///////////////////
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 import javax.servlet.http.*; import org.apache.struts.action.*; public class ChooseAction extends Action { public ActionForward execute( ActionMapping mapping, ActionForm _form, HttpServletRequest request, HttpServletResponse response ) throws Exception { ChooseForm form = (ChooseForm) _form; form.setArticle( Catalog.m_Articles[form.getIdArticle()] ); System.out.println( form.getArticle() ); return mapping.findForward("Choose"); } }
//////////////////////// la class loginaction //////////////////
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 public class ChooseForm extends ActionForm { private Article m_article; private int idarticle =1; public Article getArticle() throws IOException { return this.m_article; } public void setArticle(Article article) throws IOException { this.m_article = article; } public int getIdArticle() throws IOException { return this.idarticle; } public void setIdArticle(int IdArticle) throws IOException { this.idarticle=IdArticle; } }
///////////////// la class loginform /////////////////////
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 public class LoginAction extends Action { public ActionForward perform( ActionMapping mapping, ActionForm _form, HttpServletRequest request, HttpServletResponse response ) throws Exception { // On traite la requête cliente LoginForm form = (LoginForm) _form; System.out.println("Struts in action " + form.getUsername() + " - " + form.getPassword()); // On redirige vers la vue adaptée return mapping.findForward("Choose"); } }
///////////////// choose.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
16
17
18
19
20
21 public class LoginForm extends ActionForm { private String m_username = "root"; private String m_password = "azerty"; public String getUsername() { System.out.println("On passe par UserBean.getUsername !"); return this.m_username; } public void setUsername(String username) { System.out.println("On passe par UserBean.setUsername !"); this.m_username = username; } public String getPassword() { System.out.println("On passe par UserBean.getPassword !"); return this.m_password; } public void setPassword(String password) { System.out.println("On passe par UserBean.setPassword !"); this.m_password = password; } }
/////////////////////// 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 <%@ page language="java" import="java.util.*" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <html> <head> <title>Veuillez Choisir vos articles</title> </head> <body> <h1 align="center">Veuillez Choisir vos articles</H1> <div align="center"> Id de l'article : <bean:write name="ChooseForm" property="idArticle" /> <BR/> Marque : <bean:write name="ChooseForm" property="article.marque" /> <BR/> Designation : <bean:write name="ChooseForm" property="article.designation"/> <BR/> Prix unitaire : <bean:write name="ChooseForm" property="article.price" /> <BR /> <% int idArticle = Integer.parseInt(request.getParameter("idArticle")); %> <BR /> <a href="choose.do?idArticle=<%= idArticle - 1 %>">Précédant</a> - <a href="choose.do?idArticle=<%= idArticle + 1 %>">Suivant</a> </div> </body> </html>
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 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config> <form-beans> <form-bean name="LoginForm" type="LoginForm" /> <form-bean name="ChooseForm" type="ChooseForm" /> </form-beans> <global-forwards> <forward name="Connected" path="/Connected.jsp" /> <forward name="Choose" path="/ChooseArticle.jsp" /> </global-forwards> <action-mappings> <action path="/login" name="LoginForm" type="LoginAction" scope="request" /> <action path="/choose" name="ChooseForm" type="ChooseAction" scope="request" /> </action-mappings> </struts-config>
Partager