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
|
package controlleur.action;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.bean.Administration.CEvtBean;
import net.sf.json.JSONObject;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import dao.SessionMySQL;
public class IntituleAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception, SQLException {
String id = request.getParameter("id"); // paramètre envoyé dans la requête AJAX
// On accède à la base afin de remplir une HashMap qui contient toutes les informations de l'intitulé
SessionMySQL cnx = new SessionMySQL("mysql_utilisateur");
Connection connexion = cnx.getConnexion();
PreparedStatement stm = connexion.prepareStatement(
"SELECT * FROM tc_evt WHERE cevt_idt= ?");
stm.setInt(1, Integer.parseInt(id));
ResultSet result = stm.executeQuery();
ArrayList<CEvtBean> liste = new ArrayList<CEvtBean>();
while(result.next()) {
liste.add(new CEvtBean(
result.getInt("cevt_idt"),
result.getInt("cevt_gpfam"),
result.getString("cevt_descript"),
result.getString("cevt_lib_sal"),
result.getString("cevt_lib_dhd"),
result.getString("cevt_lib_dhf"),
result.getString("cevt_lib_suc"),
result.getString("cevt_lib_tiers"),
result.getString("cevt_lib_mat"),
result.getString("cevt_lib_val"),
result.getString("cevt_lib_unite_val"),
result.getString("cevt_lib_ddr"),
result.getString("cevt_lib_ddr_sal"),
result.getString("cevt_lib_dos")
));
}
// Nous remplissons la hashmap
HashMap hm = new HashMap();
hm.put("idt", liste.get(0).getId().toString());
hm.put("descript", liste.get(0).getDescript().toString());
hm.put("libsal", liste.get(0).getLibSal().toString());
hm.put("libdhd", liste.get(0).getLibDhd().toString());
hm.put("libdhf", liste.get(0).getLibDhf().toString());
hm.put("libsuc", liste.get(0).getLibSuc().toString());
hm.put("libtiers", liste.get(0).getLibTiers().toString());
hm.put("libmat", liste.get(0).getLibMat().toString());
hm.put("libval", liste.get(0).getLibVal().toString());
hm.put("libUval", liste.get(0).getLibUVal().toString());
hm.put("libddr", liste.get(0).getLibDdr().toString());
hm.put("libddrsal", liste.get(0).getLibddrSal().toString());
hm.put("libdos", liste.get(0).getLibDos().toString());
// chaque clé de notre map devient une clé dans l'objet JSON (utilisation de Json-lib)
JSONObject json = JSONObject.fromObject(hm);
// façon d'envoyer l'objet JSON pour que Prototype puisse le récupérer
response.setHeader("X-JSON", json.toString());
return null;
}
} |
Partager