Search code examples
jsfseam

JSF actual value input text


How can I pass the actual value in the text boxt of emailEdit (after changed) to a function or how can I bind this value to another property of a bean when changed ?

Currently I got this :

<h:inputText id="emailEdit"
value="#{portalUserHome.instance.customer.email}">
</h:inputText>

<s:link value="Send mail"
action="#{customerEdit.sendEmail}"  rerender="myCustomerEditData,myCustomerData"></s:link>

Thanks in advance


Solution

  • [Assuming you're talking about an EL function, not a JS function here]There are a few ways, but they'll mostly start with you having to bind the text to the page scope:

    <h:inputText id="emailEdit" binding="#{emailEdit}" value="#{portalUserHome.instance.customer.email}"/>
    

    From that point, you can pretty much do whatever you want, referring to the textbox using the binding of emailEdit

    • You could pass the value directly to the method you're interested in (available only starting from EL 2.2)

      <s:link value="Send mail" action="#{customerEdit.sendEmail(emailEdit.value)}" rerender="myCustomerEditData,myCustomerData"/>
      
    • You could pass the value using the f:attribute tag, and then retrieve the passed attribute in your function

      <s:link id="emailLink" value="Send mail"action="#{customerEdit.sendEmail}"  rerender="myCustomerEditData,myCustomerData">
           <f:attribute name="emailValue" value="#{emailEdit.value}"/>
      </s:link>
      

      Retrieving in your function:

      public void sendEmail(){
          FacesContext ctxt = FacesContext.getCurrentInstance();
          UIComponent comp = ctxt.getViewRoot().findComponent("emailLink"); //assuming you already have a FacesContext
          String theValue =  comp.getAttributes().get("emailValue").toString(); //obligatory null check omitted for brevity
      }