How to send an object by parameter?

4

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:selectOneMenu>

<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}" />
</p:fileUpload>

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

e.getComponent().getAttributes().get("agencia")

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

    
asked by anonymous 30.11.2015 / 18:59

1 answer

2

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:

  

XHTML Page

<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
  

BackBean

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

  

XHTML Page

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

BackBean

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

  

XHTML Page

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

BackBean

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

  

XHTML Page

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

BackBean

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!

    
18.10.2016 / 15:15