2

In JSF & Primefaces web application, I want to pass a value for the complete method of primefaces input text area control. I have tried it as follows.

JSF file

 <p:inputTextarea id="txtMicMemoVal"
             value="#{patientReportController.memoEnterVal}"
             style="min-width: 200px;" 
             completeMethod="#{investigationItemValueController.completeValues}" >
      <f:attribute name="ii" value="#{pv.investigationItem}" />
      <f:ajax event="blur"  execute="@this" 
              listener="#{patientReportController.saveMemoVal(pv.id)}" ></f:ajax>
 </p:inputTextarea>

Relevant Backing Bean

public List<String> completeValues(String qry) {
    System.out.println("completing values");
    FacesContext context = FacesContext.getCurrentInstance();
    InvestigationItem ii;
    try {
        ii = (InvestigationItem) UIComponent.getCurrentComponent(context).getAttributes().get("ii");
        System.out.println("ii = " + ii);
    } catch (Exception e) {
        ii = null;
        System.out.println("error " + e.getMessage());
    }
    Map m = new HashMap();
    String sql;
    sql = "select v.name from InvestigationItemValue v "
            + "where v.investigationItem=:ii and v.retired=false and"
            + " (upper(v.code) like :s or upper(v.name) like :s) order by v.name";
    m.put("s","'%"+ qry.toUpperCase()+"%'");
    m.put("ii", ii);
    List<String> sls = getFacade().findString(sql, m);
    System.out.println("sls = " + sls);
    return sls;
}

But the backing bean method is not fired when i enter text to input text area. But if I remove the f:attribute, backing bean is fired. But I want that parameter as well for functionality.

Thanks in advance to direct me to over come this issue.

Hatem Alimam
  • 9,968
  • 4
  • 44
  • 56
Buddhika Ariyaratne
  • 2,339
  • 6
  • 51
  • 88

1 Answers1

3

Interesting question. Primefaces bounds you to receive only a String parameter in your completion method, so the only solution I see is evaluating your expression at server side, when completion function gets called.

I suppose you've got an iteration (either ui:repeat or p:dataTable) where each id differs from the previous one. If you don't, you can also use it.

That would be the way to go:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head />
<h:body>
    <h:form>
        <ui:repeat var="str" value="#{bean.strings}">
            <p:inputTextarea value="#{bean.value}" style="min-width: 200px;"
                completeMethod="#{bean.complete}" />
        </ui:repeat>
    </h:form>
</h:body>
</html>
@ManagedBean
@RequestScoped
public class Bean {
    public String value;

    public List<String> strings = Arrays.asList("param1", "param2", "param3");

    public List<String> complete(String query) {
        List<String> results = new ArrayList<String>();
        //Here we evaluate the current #{str} value and print it out
        System.out.println(FacesContext
                .getCurrentInstance()
                .getApplication()
                .evaluateExpressionGet(FacesContext.getCurrentInstance(),
                        "#{str}", String.class));
        if (query.equals("PrimeFaces")) {
            results.add("PrimeFaces Rocks!!!");
            results.add("PrimeFaces has 100+ components.");
            results.add("PrimeFaces is lightweight.");
            results.add("PrimeFaces is easy to use.");
            results.add("PrimeFaces is developed with passion!");
        }
        return results;
    }

    public List<String> getStrings() {
        return strings;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

Note you're evaluating the current EL result for #{str} when method call performs. You'll get a different evaluation result depending on which p:inputTextArea you write to.

See also:

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217