récupérer la valeur d'un checkbox
Bonjour à vous tous,
Je veux afficher une table sur une page jsp à partir d'une table existante dans une base de données.
ma table s'affiche bien , j'ai des chekbox tel que lorsque je selectionne le chekbox un bouton "supprimer " devient enable et en cliquant sur ce bouton l'élement sélectionné sera supprimé de la base .
Mon probléme que l'appel de la méthode supprimer se fait correctement mais mon probléme réside dans la récupération de l'élément selectionné que je veux supprimer.
Pour être plus claire voici ma page 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 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 184 185 186
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%-- JSTL tag libs --%>
<%@ page contentType="text/html; charset=ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<html:html xhtml="true" locale="true">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<html:errors />
<SCRIPT language="javascript" type="text/javascript">
function setOperation(valeur){
document.forms[0].operation.value=valeur;
}
function verification()
{
var pattern = new RegExp("^[0-9]$");
if (
(document.getElementById('codPersonnel').value == "")
|| (document.getElementById('nom').value == "")
|| (document.getElementById('prenom').value == "")
|| (document.getElementById('bloque').value == "")
|| (document.getElementById('actif').value == "")
|| (document.getElementById('codeRole').value == "") )
{
alert("Il faut remplir tout les champs !");
return false;
}
if (document.getElementById('codeRole').value !=""){
if (!pattern.test(document.getElementById('codeRole').value))
{
alert('Vérifier le code Role du conseiller ');
return false;
}
if (document.getElementById('codeRole').length >1)
{
alert("Ce champ doit contenir un seul Chiffre !");
return false;
}
}
return true;
}
function submitModify(){
var conf = confirm("Etes-vous sûr de vouloir modifier cet élément ? ");
if (conf){setOperation('maj');}
else
{
setOperation('cancel');
}
}
function submitDelete(){
var conf = confirm("Etes-vous sûr de vouloir Supprimer cet élément ? ");
if (conf){setOperation('supprimer');}
else
{
setOperation('cancel');
}
}
function enableButton(){
controle=document.getElementById("controle");
modify=document.getElementById("maj");
supprimer=document.getElementById("supprimer");
if(controle.indexed ="true" )
{
modify.disabled=false;
supprimer.disabled=false;
}
}
</SCRIPT>
<body>
<html:errors />
<html:form action="/gestionConseiller" method="post">
<html:hidden property="operation" value="ajouter" />
<center><display:table
name="sessionScope.gestionConseillerList" defaultorder="descending"
pagesize="20" export="true" requestURI="">
<display:column media='html'>
<html:radio property="controle" value="codPersonnel"
onclick="enableButton()" />
</display:column>
<display:column property="codPersonnel" title="Code personnel"
sortable="true" />
<display:column property="nom" title="Nom" />
<display:column property="prenom" title="Prénom" />
<display:column property="datCrea" title="Date-création" />
</display:table></center>
<table border="0" align="center">
<tr colspan="2" aligne="center">
<td><fmt:message key="conseiller.codepersonnel" /></td>
<td><html:text property="codPersonnel" styleId="codPersonnel"
size="20" /></td>
</tr>
<tr colspan="2" aligne="center">
<td><fmt:message key="conseiller.nom" /></td>
<td><html:text property="nom" styleId="nom" size="20" /></td>
</tr>
<tr colspan="2" aligne="center">
<td><fmt:message key="conseiller.prenom" /></td>
<td><html:text property="prenom" styleId="prenom" size="20" /></td>
</tr>
<tr colspan="2" aligne="center">
<td><fmt:message key="conseiller.bloque" /></td>
<td><html:text property="bloque" styleId="bloque" size="20" /></td>
</tr>
<tr colspan="2" aligne="center">
<td><fmt:message key="conseiller.actif" /></td>
<td><html:text property="actif" styleId="actif" size="20" /></td>
</tr>
<tr colspan="2" aligne="center">
<td><fmt:message key="conseiller.coderole" /></td>
<td><html:text property="codeRole" styleId="codeRole" size="20" /></td>
</tr>
<center>
<table>
<tr>
<td>
<input type="submit" value="Ajouter"
onclick="javascript:return verification();setOperation('ajouter');" /></td>
<td><html:submit disabled="true" property="submit"
value="Supprimer" styleId="supprimer"
onclick=" submitDelete();" /></td>
<td><html:submit disabled="true" styleId="maj"
property="submit" value="Mise à jour"
onclick="javascript:return verification(); submitModify()" /></td>
<td><html:reset property="reset" value="Annuler" /></td>
</tr>
</table>
</center>
</table>
</html:form>
</body>
</html:html> |
Ma classe Formes
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
|
import org.apache.struts.action.ActionForm;
public class GestionConseillerForm extends ActionForm {
private String codPersonnel;
private String nom;
private String prenom;
private String bloque;
private String passwd;
private String actif;
private String codUser;
private String codeRole;
private String controle;
public void resetField(){
setPasswd ("");
setNom ("");
setPrenom ("");
setBloque ("");
setActif ("");
setCodUser ("");
setCodeRole ("");
}
public String getCodPersonnel() {
return codPersonnel;
}
public void setCodPersonnel(String codPersonnel) {
this.codPersonnel = codPersonnel;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public String getBloque() {
return bloque;
}
public void setBloque(String bloque) {
this.bloque = bloque;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getActif() {
return actif;
}
public void setActif(String actif) {
this.actif = actif;
}
public String getCodUser() {
return codUser;
}
public void setCodUser(String codUser) {
this.codUser = codUser;
}
public String getCodeRole() {
return codeRole;
}
public void setCodeRole(String codeRole) {
this.codeRole = codeRole;
}
public String getControle() {
return controle;
}
public void setControle(String controle) {
this.controle = controle;
}
} |
Ma Méthode supprimer dans la classe Action:
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
|
public ActionForward supprimer(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
System.out.println("Appel de la methode supprimer()");
GestionConseillerForm gestionConseillerForm= (GestionConseillerForm)form;
Utilisateur utilisateur = new Utilisateur();
String controle= gestionConseillerForm.getControle();
String codPersonnel = request.getParameter("controle");
if(codPersonnel!=null)
{
System.out.println("The box is checked and its value is " + codPersonnel);
utilisateur.setCodPersonnel(codPersonnel);
gestionConseillerService.deleteUtilisateur(utilisateur);
}
return (mapping.findForward("success"));
} |
La méthode deleteUtilisateur dans la classe DAO est la suivante :
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
public void deleteUtilisateur(Utilisateur user){
try {
System.out.println("I'm inside delete ...................");
getHibernateTemplate().delete(user);
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
} |
La trace sur la console est :
10:28:31,923 DEBUG JDBCContext:283 - after transaction completion
Appel de la methode supprimer()
The box is checked and its value is codPersonnel
10:28:40,218 DEBUG ConnectionManager:296 - opening JDBC connection
10:28:40,343 DEBUG JDBCTransaction:46 - begin
10:28:40,343 DEBUG JDBCTransaction:50 - current autocommit status: true
10:28:40,343 DEBUG JDBCTransaction:52 - disabling autocommit
I'm inside delete ...................
10:28:40,374 DEBUG JDBCTransaction:83 - commit
10:28:40,374 DEBUG AbstractBatcher:290 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
10:28:40,374 DEBUG SQL:324 - delete from STAGE.UTILISATEUR where COD_PERSONNEL=?
10:28:40,390 DEBUG ConnectionManager:369 - running Session.finalize()
10:28:40,390 DEBUG ConnectionManager:369 - running Session.finalize()
Hibernate: delete from STAGE.UTILISATEUR where COD_PERSONNEL=?
10:28:40,390 DEBUG AbstractBatcher:378 - preparing statement
10:28:40,390 DEBUG AbstractBatcher:27 - Adding to batch
10:28:40,390 DEBUG AbstractBatcher:54 - Executing batch size: 1
10:28:40,390 DEBUG AbstractBatcher:84 - success of batch update unknown: 0
10:28:40,390 DEBUG AbstractBatcher:298 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
10:28:40,390 DEBUG AbstractBatcher:416 - closing statement
10:28:40,390 DEBUG JDBCContext:278 - before transaction completion
10:28:40,390 DEBUG JDBCTransaction:173 - re-enabling autocommit
10:28:40,390 DEBUG JDBCTransaction:96 - committed JDBC Connection
10:28:40,390 DEBUG JDBCContext:283 - after transaction completion
10:28:40,390 DEBUG ConnectionManager:317 - closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
Le probléme est que lorsque je fait :
Code:
1 2
| String controle= gestionConseillerForm.getControle();
String codPersonnel = request.getParameter("controle"); |
la variable codPersonnel reste vide , jai constaté ça grâce àla trace sur al console.
Merci pour ceux qui vont m'aider ou ceux qui ont essayé de m'aider.