< p:selectOneMenu侦听器方法在选择值时返回null [英] <p:selectOneMenu listener method returns null when a value is selected

查看:97
本文介绍了< p:selectOneMenu侦听器方法在选择值时返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将JSF 2.0与Primefaces 3.4.2一起使用

I am using JSF 2.0 with Primefaces 3.4.2

我在JSF页面中有以下内容

I have the following in JSF page

<p:selectOneMenu value="#{mb.employee}">
    <f:selectItems value="#{mb.employeeList}" var="emp"
    itemLabel="#{emp.employeeName}" itemValue="#{emp.employeeCode}"/>
    <p:ajax listener="#{mb.changeMethod}" />
</p:selectOneMenu>

问题是当我在selectOneMenu中选择一个值时,对于此System.out.println("val "+employee.getEmployeeName());

Problem is when I select a value in selectOneMenu, I am getting null in changeMethod of ManagedBean, for this System.out.println("val "+employee.getEmployeeName());

这可能是什么原因?我该如何解决这个问题? 任何麻将都是非常可观的.

What could be the reason for this? How can I resolve this problem? Any hep is highly appreciable.

ManagedBean 代码

@Named("mb")
@ViewAccessScoped
public class MyBean implements Serializable {

private Employee employee;
private List<Employee> employeeList;

@Inject
EmployeeService employeeService;

@PostConstruct
public void loadEmployees() {
employeeList = employeeService.getEmployees();
}


public void changeMethod() {
System.out.println("val  "+employee.getEmployeeName());
}


/* getters and setters for employee and employeeList */
.... methods
/* */

推荐答案

您没有在<p:ajax>组件上指定要处理的表单元素,因此ajax请求可能正在提交可能与其他字段验证冲突的多个值.请记住,如果提交了一个值并且验证失败,则不会将任何请求值设置为模型.当您到达Application(Event)阶段时,模型值将不会反映任何已提交的请求值.

You are not specifying on the <p:ajax> component which form elements to process, so the ajax request may be submitting multiple values that could be conflicting with other field validation. Remmeber that if a value is submitted and it fails validation, then none of the request values get set to the model. When you reach the Application(Event) phase, the model values will not reflect any submitted request values.

尝试一下:

<p:selectOneMenu value="#{mb.employee}">
    <f:selectItems value="#{mb.employeeList}" var="emp"
    itemLabel="#{emp.employeeName}" itemValue="#{emp.employeeCode}"/>
    <p:ajax process="@this" partialSubmit="true" listener="#{mb.changeMethod}" />
</p:selectOneMenu>

上面,您将仅提交要应用于模型的当前组件请求值.

Above you will be submitting just the current component request value to be applied to the model.

实际上,这可能不是验证问题,因为ajax事件上没有提交任何请求值.

Actually it probably isn't a validation issue so much as there are no request values being submitted on the ajax event.

根据Primefaces手册:

According to the Primefaces Manual:

处理空字符串组件以在部分请求中处理.

process null String Component(s) to process in partial request.

更新null字符串组件以使用ajax更新.

update null String Component(s) to update with ajax.

第二列是默认值.在标准<f:ajax>中,execute属性默认为@this,但是对于<p:ajax>则不是这种情况.如果要提交当前组件,则必须在process属性中指定它.

The second column is Default Value. In the standard <f:ajax> the execute attribute defaults to @this however this is not the case for <p:ajax>. If you want to submit the current component then you must specify this in the process attribute.

这篇关于&lt; p:selectOneMenu侦听器方法在选择值时返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆