Vérification des champs avec JDOM
Bonjour a tous
je travaille avec struts2 et jdom
j'ai realiser ca :
un fichier xml
Code:
1 2 3 4 5
|
<?xml version="1.0" encoding="UTF-8"?>
<users>
</users> |
dans le quel j'ajoute des user avec un formulaire jsp :
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
|
<html>
<head>
<title>Inscription</title>
</head>
<body>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<center>
<s:form action="InscriptionAction.action" method="post" style="width:50%;margin:auto;background-color:green;padding-bottom:15px;">
<h2 style="text-align:center;color:white;background-color:green;"><h2>Formulaire d'inscription</h2>
<p style="text-align:center;"><s:textfield name="nom" label="Nom" size="20"></s:textfield>
<p style="text-align:center;"><s:textfield name="username" label="Prenom" size="20"></s:textfield>
<p style="text-align:center;"><s:password name="password" key="password" label="Mot de passe" size="20"></s:password>
<p style="text-align:center;"><s:textfield name="email" label="E-mail" size="20"></s:textfield>
<p style="text-align:center;"><s:textfield name="age" label="Age" size="20"></s:textfield>
<p style="text-align:center;width:50%;margin:auto;"><s:submit type="submit" value="Inscription"></s:submit>
</s:form>
</center>
</body>
</html> |
en utilisant biensur la classe inscriptionAction qui contienne le code jdom et qui permet de faire l'ajout
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 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
|
package beanAction;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import com.opensymphony.xwork2.ActionSupport;
public class InscriptionAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private int id_user;
private String username;
private String nom;
private String password;
private String email;
private String age;
static Element racine = new Element("users");
static Document register = new Document(racine);
public String execute() throws Exception {
Element user = new Element("user");
racine.addContent(user);
Element username = new Element("username");
user.addContent(username.setText(getUsername()));
Element nom = new Element("nom");
user.addContent(nom.setText(getNom()));
Element password = new Element("password");
user.addContent(password.setText(getPassword()));
Element email = new Element("email");
user.addContent(email.setText(getEmail()));
Element age = new Element("age");
user.addContent(age.setText(getAge()));
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
// sortie.output(document, System.out);
sortie.output(register, new FileOutputStream("C:/workspace/GED-1.1/ressources/login.xml"));
return null;
}
// public void register(){
//
// SAXBuilder sxb = new SAXBuilder();
// try {
// doc = sxb.build(new File("C:/workspace/GED-1.1/ressources/users.xml"));
// // document = sxb.build(new
// // File("http://localhost:8080/exist/rest/db/membres/ListeMembre"));
//
// } catch (JDOMException e1) {
// System.err.println("Fichier xml mal form�");
// } catch (IOException e2) {
// System.err.println("Erreur IO...");
//
// }
// racine = doc.getRootElement();
//
// List list = racine.getChildren("user");
//
// // System.out.println(" list.size() : " + list.size());
//
//
// for (int i = 0; i < list.size(); i++) {
// // System.out.println(" i : " + i);
// Element courant = (Element) list.get(i);
//
// if (getUsername().equals(courant.getChildText("username"))
// && getPassword().equals(courant.getChildText("password")))
//
// {
// //System.out.println("SUCCESS");
//
//
// }
// return;
// }
// getters and setters
public int getId_user() {
return id_user;
}
public void setId_user(int id_user) {
this.id_user = id_user;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
} |
et puis je parse ce fichier xml avec la classe LoginAction
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 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
|
package beanAction;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String username;
private String password;
static Document document;
static Element racine;
static boolean cnx = false;
public String execute() throws Exception {
// session.put("username",username);
SAXBuilder sxb = new SAXBuilder();
try {
document = sxb.build(new File("C:/workspace/GED-1.1/ressources/login.xml"));
} catch (JDOMException e1) {
System.err.println("Fichier xml mal form�");
} catch (IOException e2) {
System.err.println("Erreur IO...");
}
racine = document.getRootElement();
List list = racine.getChildren("user");
for (int i = 0; i < list.size(); i++) {
// System.out.println(" i : " + i);
Element courant = (Element) list.get(i);
if (getUsername().equals(courant.getChildText("username"))
&& getPassword().equals(courant.getChildText("password")))
{
cnx = true;
i = list.size();
} else {
cnx = false;
}
}
if (cnx) {
// System.out.println("SUCCESS");
return SUCCESS;
} else {
// System.out.println("ERROR");
return ERROR;
}
}
// getters and setters
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;
}
} |
avec ce formulaire login.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"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<s:form action="LoginAction" method="post">
<s:textfield name="username" label="Utilisateur" key="username"></s:textfield>
<s:password name="password" label="Mot de passe" key="password"></s:password>
<s:submit value="Se Connecter"></s:submit>
</s:form> |
pour verifier si les champs de ce formulaire corresspond ou nn au fichier xml : login.xml
mon problem c'est que je veux verifier si un utilisateur est deja existé dans le fichier ou nn
parceque dans le formulaire de login.jsp je peux me connecter avec deux username qui ont le meme mot de passe qui est deja dans le fichier xml
alors c'est pas logique
je veux verifier est ce que le champ username et le mot de passe existent deja dans le fichier login.jsp
si oui il doit m'avertir qu'il existent deja
si non il se connecte normalement
merci de m'aider