How to send an object by parameter?

Asked

Viewed 2,259 times

4

I have the 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" mode="advanced" dragDropSupport="false"
              fileUploadListener="#{fileUploadBean.uploadPhoto}" fileLimit="3" 
              multiple="true" update="messages" sizeLimit="10000000000" 
              label="Escolha o arquivo" allowTypes="/(\.|\/)(pdf)$/">  
    <f:attribute name="agencia" value="#{org}" />
</p:fileUpload>

How to pass the object agência selected and recover in fileUploadBean.uploadPhoto?


I’d do it to get a variable back:

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

In the Managedbean:

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

How to use the f:attribute to pass object that was selected in the select to the backbean?

  • 1

    Have you tried creating a method that receives this object or some attribute in which you can set it?

1 answer

3

There’s four ways for you to pass an object as a parameter via JSF:


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

Page XHTML:

<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 track parameters f:param

Page XHTML:

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

Page XHTML:

<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 track parameters f:setPropertyActionListener

Page XHTML:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.