简体   繁体   English

JSF(和PrimeFaces)如何将参数传递给ManagedBean中的方法

[英]JSF (and PrimeFaces) How to pass parameter to a method in ManagedBean

I have an Employee object I am showing in inputtext. 我有一个我在inputtext中显示的Employee对象。 For example, the firstname of the employee is shown in an inputtext. 例如,员工的名字显示在inputtext中。 When the value of this firstname changes it calls a method. 当这个名字的值改变时,它调用一个方法。 Before this is done I want to call a method which saves the ID of the employee in the managedbean so I know which employee needs to be changed. 在此之前,我想调用一个方法,该方法将员工的ID保存在managedbean中,以便我知道哪个员工需要更改。 How do I do this, I got this so far: 我怎么做到这一点,到目前为止我得到了这个:

<h:outputText value="First name:"/>
<p:inplace id="firstname" editor="true">
     <p:ajax event="save" onsuccess="#{employeeController.saveName()}"/>
     <p:inputText id="firstName" value="#{emp.firstName}"  
                  required="true" label="text"
                  valueChangeListener="#{employeeController.firstNameChanged}">
     <p:ajax event="valueChange" listener="#{employeeController.onValueChangedStart}"/>
     </p:inputText>
</p:inplace>

I guess I should pass the ID with the onValueChangedStart or firstNameChanged method. 我想我应该使用onValueChangedStart或firstNameChanged方法传递ID。 How do I do this? 我该怎么做呢? Or is there a better way to do this? 或者有更好的方法吗? There is a getter for the emp. emp有一个吸气剂。 So #{emp}.id to get it. 所以#{emp} .id得到它。

Assuming that you're indeed inside a repeating component where #{emp} is been exposed by its var attribute, you could just grab it from the EL scope inside the value change listener as follows: 假设您确实在一个重复组件中,其中#{emp}已被其var属性公开,您可以从值更改侦听器中的EL范围中获取它,如下所示:

FacesContext context = FacesContext.getCurrentInstance();
Employee employee = context.getApplication().evaluateExpressionGet(context, "#{emp}", Employee.class);
Long id = employee.getId();
// ...

Alternatively, if you wrap the value of the <h:dataTable> inside a DataModel<Employee> , then you can obtain the current row as follows: 或者,如果将<h:dataTable>value包装在DataModel<Employee> ,则可以按如下方式获取当前行:

Employee employee = model.getRowData();
Long id = employee.getId();
// ...

Unrelated to the concrete problem, it's unnecessary to have two listener methods for the value change. 具体问题无关 ,没有必要为值更改提供两个侦听器方法。 Note that the valueChangeListener gives you the opportunity to get both the old and new value by the event and the p:ajax listener not, the new value is already been set as model value at that point. 请注意, valueChangeListener使您有机会通过事件和p:ajax listener获取旧值和新值,而新值已在此时设置为模型值。 If you need both the old and new value, remove the listener attribute of <p:ajax> . 如果您需要旧值和新值,请删除<p:ajax>listener属性。 The event="valueChange" is by the way the default event already, so just remove it as well. event="valueChange"是默认事件的方式,所以也可以删除它。

Using primefaces ajax you can retrieve the value of an input in java doing: 使用primefaces ajax,您可以在java中检索输入的值:

public void onValueChanged(AjaxBehaviorEvent event)
{
    Employee employee = (Employee)((UIOutput)event.getSource()).getValue();
    //...
}

<h:selectOneMenu required="true"
    value="#{empl}" converter="#{bean.employeeConverter}">
    <f:selectItems value="#{bean.employees}" var="varEmployee" 
        itemLabel="#{varEmployee}" itemValue="#{varEmployee}"/>
    <p:ajax event="change" listener="#{bean.onValueChanged}"/>
</h:selectOneMenu>

Instead of casting to employee you cast to String since your value is a String, or use the employee Object and use a Converter or its toString() method. 因为您的值是String,所以不是强制转换为员工,而是使用员工对象并使用转换器或其toString()方法。

尝试

<p:ajax event="save" onsuccess="#{employeeController.saveName(emp.id)}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM