Bonjour,
Je suis débutant en Struts. J'essaie 'implémenter un explemple simple qui consiste à entrer un nom et prénom dans un formulaire. Le submit du formulaire est censé afficher la page :
- "logged.jsp", si les valeurs saisis correspondent à des valeurs prédéfinis "aaa" et "bbb"
- "subscribe.jsp", si valeurs sont différentes
- "erreurs.jsp" si erreurs (champ vide).
Le test d'erreurs fonctionne correctement. Mais les cas 1 et 2 ne marchent pas : j'obtiens une page blanche après envoi du formulaire.
Auriez-vous une idée su l'origine et la correction de mon problème ?
Données :
struts-1.3.10, eclipse 3.5
Voici le contenu de mes fichiers :
struts-config.xml :
Code xml : 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> <!-- beans de formulaires --> <form-beans> <form-bean name="userForm" type="com.vaannila.form.UserForm" /> </form-beans> <!-- redirections globales --> <global-forwards type="org.apache.struts.action.ActionForward"> <forward name="formulaire" path="/jsp/formulaire.jsp" redirect="false" /> </global-forwards> <!--mapping des actions --> <action-mappings> <!-- Accueil --> <action path="/inscription" type="com.vaannila.action.InscriptionAction" scope="session" name="userForm" validate="true" unknown="true" input="/jsp/erreurs.jsp"> <forward name="success" path="/jsp/logged.jsp" redirect="false"/> <forward name="failure" path="/jsp/subscribe.jsp" redirect="false" /> </action> </action-mappings> <!-- ressources --> <message-resources parameter="Resources.Messages"/> </struts-config>
fichier web.xml :
Code xml : 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 <?xml version="1.0" encoding="ISO-8859-1" ?> <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>StrutsExample2</display-name> <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> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/jsp/index.jsp</welcome-file> </welcome-file-list> </web-app>
fichier logged.jsp :
fichier subscribe.jsp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 <?xml version="1.0" encoding="UTF-8"?> <%@ page language="java" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <html:html> <head/> <body bgcolor="white" > Vous etes connecté ! </body> </html:html>
fichier InscriptionAction.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 <?xml version="1.0" encoding="UTF-8"?> <%@ page language="java" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <html:html> <head/> <body bgcolor="white" > Vous n etes pas inscrit(e). Voulez-vous vous inscrire ? ! </body> </html: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 public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ //Initialisation User user = new User() ; //Récupération du nom saisi dans un formulaire associé à la classe UserForm (voir section contrôleur String usernom = ((UserForm) form).getNom(); String userprenom = ((UserForm) form).getPrenom() ; //Traitements user.setNom(usernom ); user.setPrenom(userprenom) ; user.setInitiales(usernom.substring(0,1) + userprenom.substring(0,1)) ; // on sauve les informations dans la session, pour être accessibles par les pages JSP HttpSession session = request.getSession(); session.setAttribute("user", user); if(user.getNom().equals("aaa") && user.getPrenom().equals("bbb")){ //On redonne la main au contrôleur qui appelle la page JSP associée à la clé "success" return(mapping.findForward("success")); } return(mapping.findForward("failure"));
Partager