How to send an object by parameter?

Posted on

Question :

I have this following code snippet

<p:selectOneMenu id="agencia" converter="agenciasConverter"  value="#{agenciasMB.agencias.agencia}" style="width:150px">
    <f:selectItem itemLabel="Selecione a Agência" itemValue="" noSelectionOption="true" />
    <f:selectItems value="#{agenciasMB.lista}" var="org" itemValue="#{org}" itemLabel="#{org.agencia}"  />

<p:fileUpload id="fileIdPhoto"  fileUploadListener="#{fileUploadBean.uploadPhoto}" mode="advanced" dragDropSupport="false"
multiple="true" update="messages" label="Escolha o arquivo" sizeLimit="10000000000" fileLimit="3" allowTypes="/(.|/)(pdf)$/">  
    <f:attribute name="agencia" value="#{org}" />

I want to do the following, pass the agency object that was selected and retrieve it there in the fileUploadBean.uploadPhoto method

I used to do this to retrieve a variable:

<f:attribute name="agencia" value="minhavariavel" />

And there in MB I gave


but what happens inside the f: attribute so that it passes the entire object that was selected in the select?


Answer :

There are four ways you can pass an object as a parameter via JSF:

1. Since version 2.0 of JSF you can pass parameters as follows:



<h:commandButton action="#{testeBean.testarParametroString('Parâmetro Enviado')}" />
<h:commandButton action="#{testeBean.testarParametroNumero(2016)}" />
<h:commandButton action="#{testeBean.testarParametroObjeto(carro)}" />
// O botão pode estar dentro de uma tabela de objetos do tipo carro



public void testarParametroString(String palavra) {
    System.out.println(">>> Verificando parâmetro recebido: " + palavra);

public void testarParametroNumero(Integer numero) {
    System.out.println(">>> Verificando parâmetro recebido: " + numero);

public void testarParametroObjeto(Carro carro) {
    System.out.println(">>> Verificando parâmetro recebido: " + carro);

Note: If you use Tomcat be sure to include the library: el-impl-2.2.jar

2. Passing parameters via f: param



<h:commandButton action="#{testeBean.testar}">
    <f:param name="parametro" value="Parâmetro Enviado" />



public void testar() {
    Map<String,String> p = FacesContext.getExternalContext().getRequestParameterMap();
    String parametroRecebido = p.get("parametro");
    System.out.println(">>> Verificando parâmetro recebido: " + parametroRecebido);

3. Passing parameters via f: attribute



<h:commandButton action="#{testeBean.executarAcao}" actionListener="#{testeBean.testar}">
    <f:attribute name="parametro" value="testandoParametro" />



public void testar(ActionEvent event) {
    String parametroRecebido;
    parametroRecebido = (String) event.getComponent().getAttributes().get("parametro");

public void executarAcao() {
    //Ação a ser realizada ao clicar no botão...

4. Passing parameters via f: setPropertyActionListener



<h:commandButton action="#{testeBean.testar}" >
    <f:setPropertyActionListener target="#{testeBean.parametro}" value="Teste" />



public String parametro;

public void testar() {
    //Ação a ser realizada ao clicar no botão...

public void setParametro(String parametroRecebido) {
    this.parametro = parametroRecebido;

More information:

I hope this helps …

Good luck!


Leave a Reply

Your email address will not be published. Required fields are marked *