Formulaire de login avec struts2 et xml
Bonjour,
hier j'ai réussi a réaliser un formulaire de connexion avec struts 2 en utilisant une action bean
voila les détails :
voila la classe login :
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
| import com.opensymphony.xwork2.ActionSupport;
public class LoginValidation extends ActionSupport{
private String username;
private String password;
public String execute() throws Exception {
if(this.getUsername().equals("admin")&& this.getPassword().equals("admin"))
{
return SUCCESS;
}
else
{
return ERROR;
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
} |
fichier struts.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<package name="sample" extends="struts-default">
<action name="index" class="roseindia.LoginValidation">
<result name="success">/jsp/welcome.jsp</result>
<result name="error">/jsp/error.jsp</result>
</action>
<action name="login">
<result name="success">./jsp/login.jsp</result>
</action>
</package>
</struts> |
web.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?xml version="1.0" encoding="UTF-8"?>
<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>SendMessage</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app> |
login.jsp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Insert message</title>
</head>
<body>
<h2>Please enter User name & Password</h2>
<s:form action="index" method="post">
<s:textfield name="username" key="username"></s:textfield>
<s:password name="password" key="password"></s:password>
<s:submit></s:submit>
</s:form>
</body>
</html> |
error.jsp
Code:
1 2 3 4 5 6 7 8 9 10 11
| <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Login et Mot de passe sont invalide . S'ils vous plait entrez le Login et Mot de Passe correct.<br></br>
<a href="jsp/login.jsp"> Essayer encore</a>
</body>
</html> |
welcome.jsp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<H1>....... Login user Information ........</H1><br/>
Name : <s:property value="username"/><br></br>
Password : <s:property value="password"/>
<h2>----------Login success------------</h2>
</body>
</html> |
maintenant je veux utiliser un fichier xml qui stocke : username et password comme ce ci :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<BDD>
<login>
<username>yassine</username>
<password>123</password>
</login>
<login>
<username>ahmed</username>
<password>456</password>
</login>
<login>
<username>kamal</username>
<password>789</password>
</login>
</BDD> |
Pour vérifier si username et password saisi dans le formulaire sont identiques a celui du fichier xml si non j'affiche un message d'erreur. Je travaille avec struts2 , est ce que c'est possible d'utiliser un parser xml comme JDOM pour faire ça ?
Merci