jsf - 将参数传递给 primefaces inputtextarea 控件的完整方法

标签 jsf primefaces

在 JSF 和 Primefaces Web 应用程序中,我想为 primefaces 输入文本区域控件的完整方法传递一个值。我已经尝试过如下。

JSF 文件

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

相关支持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;
}

但是当我在输入文本区域输入文本时,辅助 bean 方法不会被触发。但如果我删除 f:attribute,则会触发支持 bean。但我也想要该参数来实现功能。

提前感谢您指导我解决这个问题。

最佳答案

有趣的问题。 Primefaces 限制您在完成方法中仅接收 String 参数,因此我看到的唯一解决方案是在调用完成函数时在服务器端评估您的表达式。

我想您有一个迭代(ui:repeatp:dataTable),其中每个 id 都与前一个不同。如果没有,您也可以使用它。

这就是要走的路:

<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;
    }

}

请注意,当执行方法调用时,您正在评估 #{str} 的当前 EL 结果。根据您写入的 p:inputTextArea,您将获得不同的评估结果。

另请参阅:

关于jsf - 将参数传递给 primefaces inputtextarea 控件的完整方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21192876/

相关文章:

java - 即使 onclick 为空,也可以在 commandButton 中使用 "oncomplete"吗?

jsf - p :selectOneMenu dropdown part scrolls and does not stay in position

java - ICEfaces : Row selection listener method does't fires in ICEfaces

java - PrimeFaces v3.5 : Dialog is not shown using RequestContext on non-Ajax calls

java - 有没有办法将样式应用于 p :selectManyMenu? 中的列

jsf-2 - 在复合组件中获取正确的父 ID

javascript - 从 JavaScript + JSF 调用 Backing bean 方法

html - 如何让JSF通过HTML属性

JSF:将页面动态包含到 primefaces 对话框中

css - 更改命令链接标题的样式