0

i've got this jsf simple form in which i try if the method for set the id of the selected invoice works:

    <h:form>

        <fieldset>
            <label for="email">E-Mail</label> 
            <h:inputText id="email" value="#{invoiceManageController.selectedInvoiceId}"></h:inputText>

            <h:commandButton styleClass="button-primary" action="#{invoiceManageController.putInvoice()}" value="Login"></h:commandButton>
            <h:outputText value="#{ invoiceSessionBean.invoiceId}" />  
        </fieldset>

   </h:form>

then i got a controller class that works on a bean:

@Model
public class InvoiceManageController {

    @Inject
    private InvoiceDao invoiceDao;

    @Inject
    private InvoiceSessionBean invoiceSession;

    private List<Invoice> lastInvoicesData;

    private Long selectedInvoiceId;

    public InvoiceManageController(){

         selectedInvoiceId = new Long(3);

    }

    public void putInvoice(){


        invoiceSession.setInvoiceId(selectedInvoiceId);


    }

    public void setSelectedInvoiceId(Long selectedInvoiceId) {
        this.selectedInvoiceId = selectedInvoiceId;
    }



    public Long getSelectedInvoiceId() {
        return selectedInvoiceId;
    }

}

and finally i got a simple bean that save the id of the invoice selected during a session:

import javax.faces.bean.SessionScoped;
import javax.inject.Named;

@SessionScoped
@Named
public class InvoiceSessionBean implements Serializable{

    private static final long serialVersionUID = 1L;

    private Long invoiceId;

    public Long getInvoiceId() {
        return invoiceId;
    }

    public void setInvoiceId(Long invoiceId) {

        this.invoiceId = invoiceId;

    }

}

when i click on the button the method isn't called and the outputText is not update, why? instead if i call for example other methods that not use InvoiceSessionBean work,why?

My test:

If i replace the annotation on the InvoiceSessionBean with:

@ManagedBean
@SessionScoped

and try to access directly to the bean changing the form to:

it works why?

     <h:form>
        <fieldset>
            <label for="email">E-Mail</label> 
            <h:inputText id="email" value="#{ invoiceSessionBean.invoiceId }"></h:inputText>

            <h:commandButton styleClass="button-primary"  action="#{invoiceSessionBean.setInvoiceId(invoiceSessionBean.invoiceId)}" />
           <h:outputLabel value="#{ invoiceSessionBean.invoiceId }" />  
        </fieldset>

  </h:form>

Solved: My problem was that i was minxing CDI beans with JSF beans thus i change the import and works

 import javax.enterprise.context.SessionScoped;
 import javax.inject.Named;
Alberto Merciai
  • 474
  • 1
  • 5
  • 17
  • Where's the method ectaaaaaInvoice in your controller? – Alexey Soshin Jun 12 '17 at 16:28
  • @AlexeySoshin sorry i was wrong – Alberto Merciai Jun 12 '17 at 16:29
  • 1
    So it works now? If I remember right, `@ManagedBean` has a different lifecycle that `@Named`. I think that after the value gets set, an `@Named` bean will be recreated new and the value will be lost. You can put an `@PostConstruct` and logging to check this. `@Named` is a CDI annotation and `@ManagedBean` is a JSF annotation. https://stackoverflow.com/questions/11986847/java-ee-6-javax-annotation-managedbean-vs-javax-inject-named-vs-javax-faces – K.Nicholas Jun 12 '17 at 18:22
  • With this method works but i want run it with the first method and figure out why with that not works, thanks – Alberto Merciai Jun 12 '17 at 18:43

0 Answers0