[Débutant] problème de redirection
Bonjour,
je veux réaliser ma première application avec hibernate et struts, pour hibernate, je crois avoir réglé tous mes problèmes, maintenant j'ai décidé de me lancer dan struts.
Pour cela j'ai chopé un tuto tout simple sur le net, je l'ai bien lu et j'ai essayer de mettre ce que j'ai appris en pratique :
L'application de teste que je voulais réaliser consiste en une page Login.jsp, avec un mot de identifiant et un mot de passe, qui redirige vers une autre page Connected.jsp qui est sensé juste afficher un gentil Bonjour et ré-afficher l'identifiant saisi dans la première page.
Bon, si je met un poste sur ce forum, c'est qu'il y a un ic ;).
mon problème est que lorsque je clique sur le bouton « submit » ce dernier m'envoie sur une page blanche dont l'adresse est localhost.../login.do
Je ne comprend pas pourquoi la redirection ne se fait pas bien alors que j'ai édité mon fichier struts-config.xml pour assurer cette redirection.
Je vous prie de bien vouloir m'aider à résoudre ce problème, je suis un débutant struts et je ne trouve pas de solution sur le net.
Pour m'aider à résoudre mon problème, voici les fichier principaux de mon « application » :
Login.jsp :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
<%@ page language="java" import="java.util.*"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html>
<head>
<title>Veuillez vous identifier</title>
</head>
<body>
<h1 align="center">Veuillez votre identifier</H1>
<div align="center"><html:form action="login" method="POST"
focus="username">
identifiant : <html:text property="username" />
<BR />
mot de passe : <html:password property="password" />
<BR />
<html:submit property="submit" />
</html:form></div>
</body>
</html> |
Cennected.jsp :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
<%@ page language="java" import="java.util.*"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<html>
<head>
<title>Confirmation de connexion</title>
</head>
<body>
<h1 align="center">Confirmation de connexion</H1>
<div align="center">Bonjour Mr <bean:write name="LoginForm"
property="username" /></div>
</body>
</html> |
LoginAction.java :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
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 (mais ça ne marche pas ici :'(
return mapping.findForward("Connected");
}
} |
LoginForm.java :
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 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
|
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
/**
* @author Amine
*
*/
public class LoginForm extends ActionForm {
public static final long serialVersionUID = -10;
private String username = "root";
private String password = "azerty";
public String getUsername() {
System.out.println("On passe par UserBean.getUsername !");
return this.username;
}
public void setUsername(String username) {
System.out.println("On passe par UserBean.setUsername !");
this.username = username;
}
public String getPassword() {
System.out.println("On passe par UserBean.getPassword !");
return this.password;
}
public void setPassword(String password) {
System.out.println("On passe par UserBean.setPassword !");
this.password = password;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (this.getUsername() == null || this.getUsername().equals("")) {
errors.add("username", new ActionMessage("Veuillez saisir votre identifiant"));
System.out.println("Error : bad username");
}
if (this.getPassword() == null || this.getPassword().equals("")) {
errors.add("password", new ActionMessage("Veuillez saisir votre mot de passe"));
System.out.println("Error : bad password");
}
return errors;
}
} |
web.xml :
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 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
|
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
...
<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>2</param-value>
</init-param>
<init-param>
<param-name>application</param-name>
<param-value>ApplicationResources</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
...
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
<taglib-location>
/WEB-INF/struts-nested.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
</jsp-config>
</web-app> |
merci d'avance pour votre aide.