struts et base de donnees
Salut tout le monde,
Je travaille avec eclipse 3.1,struts, hibernate et MySql, et je veux inserer les donnees d'un formulaire dans la base de donnees.
Voici les fichiers que j'utilise:
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
| <%@ page contentType="text/html; charset=Cp1256" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-nested" prefix="nested" %>
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Cp1256"/>
<title></title>
<script language="JavaScript">
function ajouter() {
document.forms[0].submit();
}
</script>
</head>
<body>
<html:form method="POST" action="/Confirm">
<bean:message key="projet.message"/>
<br>
<html:text property="prenom"></html:text>
<br>
<html:text property="nom"></html:text>
<br>
<html:button property="confirmer" value="Confirmer" onclick="ajouter();"></html:button>
</html:form>
</body>
</html:html> |
Succes.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
| <%@ page contentType="text/html; charset=Cp1256" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-nested" prefix="nested" %>
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Cp1256"/>
<title></title>
</head>
<body>
<html:form method="POST" action="/resultat">
Succes
<%
String nom=(String)request.getParameter("nom");
String prenom=(String)request.getParameter("prenom");
%>
<br>
Nom : <%=nom%>
<br>
Prenom : <%=prenom%>
</html:form>
</body>
</html:html> |
FormulaireForm.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
| package src;
import org.apache.struts.action.ActionForm;
public class FormulaireForm extends ActionForm{
/**
*
*/
private static final long serialVersionUID = 1L;
private String nom;
private String prenom;
public FormulaireForm(){
}
public String getNom(){
return nom;
}
public void setNom(String nom){
nom=this.nom;
}
public String getPrenom(){
return prenom;
}
public void setPrenom(String prenom){
prenom=this.prenom;
}
/*public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (getNom() == null || getPrenom()==null) {
FormulaireForm f=new FormulaireForm();
request.setAttribute("FormulaireForm",f);
errors.add("name", new ActionMessage("error.name.required"));
// TODO: add 'error.name.required' key to your resources
}
else {
System.out.println("Le nom est:"+getNom());
System.out.println("Le prenom est:"+getPrenom());
}
return errors;
}*/
} |
FormulaireAction.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
| package src;
import metier.AjoutOrphelin;
import org.apache.struts.action.Action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
public class FormulaireAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
FormulaireForm f=(FormulaireForm)form;
String nom=f.getNom();
String prenom=f.getPrenom();
System.out.println("Le prenom est "+prenom);
System.out.println("Le nom est "+nom);
//request.setAttribute("nom",nom);
//request.setAttribute("prenom",prenom);
AjoutOrphelin ajout=new AjoutOrphelin(f,request);
ajout.Ajout();
return (mapping.findForward("resultat"));
}
} |
Orphelin.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
| package donnees;
import donnees.base.BaseOrphelin;
public class Orphelin extends BaseOrphelin {
private static final long serialVersionUID = 1L;
/*[CONSTRUCTOR MARKER BEGIN]*/
public Orphelin () {
super();
}
/**
* Constructor for primary key
*/
public Orphelin (java.lang.Integer id) {
super(id);
}
/**
* Constructor for required fields
*/
public Orphelin (
java.lang.Integer id,
java.lang.String nomOrphelin,
java.lang.String prenomOrphelin) {
super (
id,
nomOrphelin,
prenomOrphelin);
}
/*[CONSTRUCTOR MARKER END]*/
} |
BaseOrphelin.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 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| package donnees.base;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
/**
* This is an object that contains data related to the orphelin table.
* Do not modify this class because it will be overwritten if the configuration file
* related to this class is modified.
*
* @hibernate.class
* table="orphelin"
*/
public abstract class BaseOrphelin implements Serializable {
public static String REF = "Orphelin";
public static String PROP_NOM_ORPHELIN = "NomOrphelin";
public static String PROP_PRENOM_ORPHELIN = "PrenomOrphelin";
public static String PROP_ID = "Id";
// constructors
public BaseOrphelin () {
initialize();
}
/**
* Constructor for primary key
*/
public BaseOrphelin (java.lang.Integer id) {
this.setId(id);
initialize();
}
/**
* Constructor for required fields
*/
public BaseOrphelin (
java.lang.Integer id,
java.lang.String nomOrphelin,
java.lang.String prenomOrphelin) {
this.setId(id);
this.setNomOrphelin(nomOrphelin);
this.setPrenomOrphelin(prenomOrphelin);
initialize();
}
protected void initialize () {}
private int hashCode = Integer.MIN_VALUE;
// primary key
private java.lang.Integer id;
// fields
private java.lang.String nomOrphelin;
private java.lang.String prenomOrphelin;
// collections
//private java.util.Set<orph_package.Garant> garants;
private Set garants = new HashSet();
public Set getGarants() {
return garants;
}
public void setGarants(Set garants) {
this.garants = garants;
}
/**
* Return the unique identifier of this class
* @hibernate.id
* generator-class="increment"
* column="ID_orphelin"
*/
public java.lang.Integer getId () {
return id;
}
/**
* Set the unique identifier of this class
* @param id the new ID
*/
public void setId (java.lang.Integer id) {
this.id = id;
this.hashCode = Integer.MIN_VALUE;
}
/**
* Return the value associated with the column: nom_orphelin
*/
public java.lang.String getNomOrphelin () {
return nomOrphelin;
}
/**
* Set the value related to the column: nom_orphelin
* @param nomOrphelin the nom_orphelin value
*/
public void setNomOrphelin (java.lang.String nomOrphelin) {
this.nomOrphelin = nomOrphelin;
}
/**
* Return the value associated with the column: prenom_orphelin
*/
public java.lang.String getPrenomOrphelin () {
return prenomOrphelin;
}
/**
* Set the value related to the column: prenom_orphelin
* @param prenomOrphelin the prenom_orphelin value
*/
public void setPrenomOrphelin (java.lang.String prenomOrphelin) {
this.prenomOrphelin = prenomOrphelin;
}
/**
* Return the value associated with the column: Garants
*/
/*public java.util.Set<orph_package.Garant> getGarants () {
return garants;
}
*/
/**
* Set the value related to the column: Garants
* @param garants the Garants value
*/
/*public void setGarants (java.util.Set<orph_package.Garant> garants) {
this.garants = garants;
}
public void addToGarants (orph_package.Garant garant) {
if (null == getGarants()) setGarants(new java.util.TreeSet<orph_package.Garant>());
getGarants().add(garant);
}
*/
public boolean equals (Object obj) {
if (null == obj) return false;
if (!(obj instanceof donnees.Orphelin)) return false;
else {
donnees.Orphelin orphelin = (donnees.Orphelin) obj;
if (null == this.getId() || null == orphelin.getId()) return false;
else return (this.getId().equals(orphelin.getId()));
}
}
public int hashCode () {
if (Integer.MIN_VALUE == this.hashCode) {
if (null == this.getId()) return super.hashCode();
else {
String hashStr = this.getClass().getName() + ":" + this.getId().hashCode();
this.hashCode = hashStr.hashCode();
}
}
return this.hashCode;
}
public String toString () {
return super.toString();
}
} |
HibernateUtil.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
| package donnees;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Crée la SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Problème de configuration : " + ex.getMessage(), ex);
}}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Ouvre une nouvelle Session, si ce Thread n'en a aucune
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
} |
Lorsque je fais le test d'ajout sans passer par struts, l'insertion dans la base de donnees se fait parfaitement.
et lorsque je travaille avec struts sans base de donnees, j'arrive a afficher les valeurs saisies a partir du formulaire dans la page "succes.jsp".
Le probeleme se pose lorsque je travaille avec les deux. Deja, avec le debuggage, j'ai decouvert que les valeurs getPrenom() et getNom() dans FormulaireAction sont nulles.
En plus, il me declare une erreur qu'il ne connait pas la methode Ajout().
Est ce que qlq peut me dire si la facon avec laquelle je procede est correcte. sinon, c koi la solution du probleme. Merci d'avance.