Java JSF(和 PrimeFaces)如何将参数传递给 ManagedBean 中的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9805276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSF (and PrimeFaces) How to pass parameter to a method in ManagedBean
提问by Klaasvaak
I have an Employee object I am showing in inputtext. For example, the firstname of the employee is shown in an 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. How do I do this, I got this so far:
我有一个在输入文本中显示的 Employee 对象。例如,员工的名字显示在输入文本中。当这个名字的值改变时,它调用一个方法。在完成此操作之前,我想调用一个方法,该方法将员工的 ID 保存在 managedbean 中,以便我知道需要更改哪个员工。我该怎么做,到目前为止我得到了这个:
<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. How do I do this? Or is there a better way to do this? There is a getter for the emp. So #{emp}.id to get it.
我想我应该使用 onValueChangedStart 或 firstNameChanged 方法传递 ID。我该怎么做呢?或者有没有更好的方法来做到这一点?emp 有一个吸气剂。所以 #{emp}.id 得到它。
采纳答案by BalusC
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:
另外,如果你包裹value
的<h:dataTable>
内部DataModel<Employee>
,那么你就可以得到当前行如下:
Employee employee = model.getRowData();
Long id = employee.getId();
// ...
Unrelatedto 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. If you need both the old and new value, remove the listener
attribute of <p:ajax>
. The event="valueChange"
is by the way the default event already, so just remove it as well.
与具体问题无关,没有必要有两个值变化的监听器方法。请注意,这valueChangeListener
使您有机会通过事件获取旧值和新值,而p:ajax listener
不是,新值此时已被设置为模型值。如果您需要旧值和新值,请删除 的listener
属性<p:ajax>
。该event="valueChange"
是顺便把默认的事件了,所以只是删除它。
回答by djmj
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.
因为您的值是一个字符串,所以不是强制转换为员工,而是强制转换为字符串,或者使用员工对象并使用 Converter 或其 toString() 方法。
回答by Kragh
try
尝试
<p:ajax event="save" onsuccess="#{employeeController.saveName(emp.id)}"/>