dimanche 13 octobre 2019

I need to understand how to get values from checkbox or checkboxlist

I'm stuck with this data collection to create a new user with only some options that I allow them to add.

The idea, is to have a JSP page where the user introduces the name of the user, chooses one especiality and checks from none to all additional capabilities. All this info will be stored in a MySQL DB.

I have the first page working, getting the data from DB perfectly, but I'm unable to get to the destination page properly. I want to have each capability in a different row for better User Inteface.

This is my starting JSP page:

<s:form action="insercionActionProfe">
    s:textfield name="nombre" required="" align="center" label="Nombre Completo" placeHolder="Nombre Completo"/><br><br>
    <s:select name="Especialidad" list="listaEspecialidades" label="Especialidad" headerKey="" headerValue="-- Escoge especialidad --"/><br><br>
    Escoge las habilitaciones que tiene este profesor:<br><br>
    <s:hidden name="numSustitutciones" value="0"/>
    <s:hidden name="Alta" value="Si"/>

    <table>
        <s:iterator value="listaHabilitaciones" var="habilitacion" status="rowstatus">
        <tr>
            <td style="text-align: center">
                <s:label>
                <input type="checkbox" name="habilitacionSeleccionada" value="${habilitacion}"                              id="habilitacionSeleccionada-${rowstatus.index}" />        
                            </s:label>
            </td>
            <s:label>
            <td style="text-align: left"><s:property value="#habilitacion"/></td>
            </s:label>
        </tr>
        </s:iterator>
    </table>
    <s:submit value="Insertar" align="center"/>
</s:form>   

This is my Action class:

public class AltaProfe extends ActionSupport {
    private String profeAlta;

    private List<String> listaEspecialidades = new ArrayList();
    private List<String> listaAsignaturasHabilitadas = new ArrayList();
    private List<String> listaAsignaturasHabilitadasSeleccionadas = new ArrayList();

    // getters & setters


    public AltaProfe(){}

    public List<String> getListaHabilitaciones(){

            List <String> listaHabilitaciones = new ArrayList();
            // get data from DB
            return listaHabilitaciones;
    }
    @Override
    public String execute() throws Exception{

            listaAsignaturasHabilitadas = getListaHabilitaciones();
            listaEspecialidades = listaAsignaturasHabilitadas;
            return "OK";
    }

    public String display() {
            return "NONE";
    }
}

And this is the ModelDriven class:

public class InsercionActionProfe extends ActionSupport implements ModelDriven<Profesor>,Validateable{

    private Profesor profe = new Profesor();

    @Override
    public Profesor getModel() { return profe; }

    @Override
    public String execute() throws Exception{ 
    return "OK"; 
    }

    @Override
    public void validate() {  }

    public String insercionProfesor(){

    try{
        switch (profe.insercionProfesor()){
        case "OK":
            return "OK";
        case "DUPLICADA":
            return "DUPLICADA";
        case "ERROR":
            return "ERROR";
        default:
            return "ERROR";
        }
    } catch (Exception e){
        System.out.println("Error en el insercionProfesor "+e);
    }
    return "INPUT";
    }
}

This is my Model class:

public class Profesor {

    private String nombre;
    private String especialidad;
    private boolean alta;
    private int numSustituciones;
    private List<String> habilitaciones;
    private List<String> habilitacionesSeleccionadas = new ArrayList<>();

    // getters & setters

    public Profesor() {
    this.especialidad = "";
    this.nombre="";
    this.alta = true;
    this.numSustituciones = 0;
    AltaProfe aP = new AltaProfe();
    this.habilitaciones = aP.getListaHabilitaciones();
    habilitaciones.stream().forEach((h) -> {
        this.habilitacionesSeleccionadas.add(h);
    });

    }

    public Profesor(String nombre, String especialidad, boolean alta, int numSustituciones, List<String> habilitaciones, ArrayList<String> habilitacionesSeleccionadas){
       this.nombre = nombre;
       this.especialidad = especialidad;
       this.alta = alta;
       this.numSustituciones = numSustituciones;
       this.habilitaciones = habilitaciones;
       this.habilitacionesSeleccionadas = habilitacionesSeleccionadas;
    }

    public String insercionProfesor(){

    // insert method to put the data in the DB.

    }
}

Finally, the landing JSP page (which I never reached) tries to get this info but now I'm having a NoSuchMethodException so I never to get result page which should be something like:

Name of the user <s:property value="nombre"/>
Capabilities <s:property value="especialidad"/>

Once the NoSuchMethodException is solved, how can I do get shown the list called habilitaciones (the one with the checkbox)? And, is there any change that I must be doing to get this working? Like change any variable type, any transition, etc...

I'm really lost on this, please, please, please help me on this.




Aucun commentaire:

Enregistrer un commentaire