I have a functionality to sign up to an activity, for this, in each activity I have a sign up button, which calls the corresponding controller:
Below the button there is an input field in which I indicate the id of the activity to which the user wants to sign up and next to the greeting we see the user's id.
<h:column>
<f:facet name = "header">Apuntarse</f:facet>
<h:commandButton value="Apuntarse" action="#{controladorParticipaciones.apuntarse}" />
<h:inputHidden id="idActividad" value="#{actividad.id}"/>
</h:column>
And the controller code is:
package controlador.control_Participaciones;
import java.util.List;
import javax.annotation.Resource;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.sql.DataSource;
import modelo.Actividad;
/**
* Servlet implementation class ControladorActividades
*/
@WebServlet("/ControladorParticipaciones")
@Named
@SessionScoped
public class ControladorParticipaciones extends HttpServlet {
private static final long serialVersionUID = 1L;
@Resource(lookup="jdbc/practica1")
private DataSource miPool;
private long idUsuario;
private long idActividad;
private Actividad activity;
public String apuntarse() {
FacesContext context = FacesContext.getCurrentInstance();
boolean encontrada = false;
@SuppressWarnings("unchecked")
List<Actividad> actividades = (List<Actividad>) context.getExternalContext().getSessionMap().get("actividades");
for (Actividad actividad_aux : actividades) {
if(actividad_aux.getId() == idActividad) {
context.getExternalContext().getSessionMap().put("actividad", actividad_aux);
activity = actividad_aux;
encontrada = true;
break;
}
}
if(!encontrada) {
context.getExternalContext().getSessionMap().put("msg", "No hemos podido encontrar la actividad");
return "error";
}
return "apuntarse";
}
public long getIdUsuario() {
return idUsuario;
}
public void setIdUsuario(long idUsuario) {
this.idUsuario = idUsuario;
}
public long getIdActividad() {
return idActividad;
}
public void setIdActividad(long idActividad) {
this.idActividad = idActividad;
}
public Actividad getActivity() {
return activity;
}
public void setActivity(Actividad activity) {
this.activity = activity;
}
}
The value of the variable indicated with an arrow is always 0, but I want it to be the value of the input field. JSF fill in the input, since the input's Value field is different...
How can I pass the value of the activity's id to the controller? either with an input or otherwise.
Answering my question, I have found several ways to do it, but I think the most efficient is to send the values as parameters to the method:
And in the controller:
Simply that your sign up method receives the idActivity parameter and you pass it directly from the action. You don't need the hidden input:
And the controller:
You could also pass the activity object if that's more convenient. You can update this data via f:ajax in the commandButtom.
EDITION:
Be careful if your method returns a string in this case "sign up" you will refresh the entire page. Instead if you return NULL you stay in it and you could use f:ajax to update some data.
EDIT 2:
The truth is I don't know if it's inputHidden, supposedly it's not visible but it's displayed in your graph.
EDIT 3: If it's inputHidden then Pass the activity object and get rid of all the searching:
And the controller: